文章目录
冒泡排序
package sort;
public class bubbleSort {
public static void main(String[] arg) {
int a[] = {49,38,65,97,76,13,27,49,78,34,12,64,
5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
int temp = 0;
for(int i = 0; i < a.length - 1; i++){
for(int j = 0;j < a.length - 1 - i;j++){
if( a[j] > a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(int i = 0;i < a.length;i++){
System.out.println(a[i]);
}
}
}
堆排序 - heap sort
package sort;
import java.util.Arrays;
public class HeapSort {
public void heapSort(int[] a){
System.out.println("开始排序");
int arrayLength = a.length;
for(int i = 0;i < arrayLength - 1;i++){
buildMaxHeap(a,arrayLength - 1 - i);
swap(a,0,arrayLength - 1 - i);
System.out.println(Arrays.toString(a));
}
System.out.println("Final: " + Arrays.toString(a));
}
private void swap(int[] data, int i, int j) {
int tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
private void buildMaxHeap(int[] data, int lastIndex) {
for(int i = (lastIndex - 1)/2; i >= 0; i--){
int k = i;
while(k*2 + 1 <= lastIndex){
int biggerIndex = 2*k + 1;
if(biggerIndex < lastIndex){
if(data[biggerIndex] < data[biggerIndex+1 ]){
biggerIndex++;
}
}
if(data[k] < data[biggerIndex]){
swap(data,k,biggerIndex);
k = biggerIndex;
}
else{
break;
}
}
}
}
public static void main(String[] args) {
int a[] ={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
HeapSort tool = new HeapSort();
tool.heapSort(a);
}
}
插入排序 - insert sort
package sort;
public class insertSort {
public static void main(String[] args) {
int a[] = {49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
int temp = 0;
for(int i = 1; i < a.length; i++){
int j = i-1;
temp = a[i];
for(; j >= 0 && temp < a[j]; j--){
a[j+1] = a[j];
}
a[j+1] = temp;
}
for(int i=0;i<a.length;i++){
System.out.println(a[i]);
}
}
}
归并排序 - merge sort
package sort;
import java.util.Arrays;
public class mergeSort {
public mergeSort(int[] a){
sort(a,0,a.length-1);
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
public void sort(int[] data, int left, int right) {
if( left < right){
int center = (left + right)/2;
sort(data,left,center);
sort(data,center + 1,right);
merge(data,left,center,right);
}
}
public void merge(int[] data, int left, int center, int right) {
int [] tmpArr= new int[data.length];
int mid = center + 1;
int third = left;
int tmp = left;
while(left <= center && mid <= right){
if(data[left] <= data[mid]){
tmpArr[third++] = data[left++];
}
else{
tmpArr[third++] = data[mid++];
}
}
while(mid<=right){
tmpArr[third++] = data[mid++];
}
while(left<=center){
tmpArr[third++] = data[left++];
}
while(tmp<=right){
data[tmp] = tmpArr[tmp++];
}
System.out.println(Arrays.toString(data));
}
public static void main(String[] arg) {
int a[] = {49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
mergeSort tool = new mergeSort(a);
tool.hashCode();
}
}
快速排序 - quick sort
package sort;
public class quickSort {
public quickSort(int[] a){
System.out.println("Jerry Main Entry point for quickSort: ");
quick(a);
for(int i = 0;i < a.length;i++){
System.out.println(a[i]);
}
}
public int getMiddle(int[] list, int low, int high) {
System.out.println(" Jerry I am in getMiddle, low: " + low + " high: " + high);
int tmp = list[low];
while (low < high){
while (low < high && list[high] >= tmp) {
System.out.println("tmp: " + tmp + " list[high]: " + list[high]);
high--;
}
System.out.println("Move list[high]: " + list[high] + " to list[low]: " + list[low]);
list[low] =list[high];
while (low < high&& list[low] <= tmp) {
low++;
}
System.out.println("Move list[low]: " + list[low] + " to list[high]: " + list[high]);
list[high] =list[low];
}
System.out.println("Final !!");
list[low] = tmp;
return low;
}
public void _quickSort(int[] list, int low, int high) {
System.out.println("Jerry I am in list: " + list + " low: " + low + " high: " + high);
if (low < high){
int middle =getMiddle(list, low, high);
_quickSort(list, low, middle - 1);
_quickSort(list,middle + 1, high);
}
}
public void quick(int[] a2) {
if (a2.length > 0) {
_quickSort(a2,0, a2.length - 1);
}
}
public static void main(String[] args) {
int a[] = {3,2,4,1};
quickSort tool = new quickSort(a);
tool.hashCode();
System.out.println("ok");
}
}
另一种快速排序
package sort;
public class QuickSort2 {
private int[] numbers;
private int number;
public void sort(int[] values) {
if (values == null || values.length == 0){
return;
}
this.numbers = values;
number = values.length;
quicksort(0, number - 1);
}
private void quicksort(int low, int high) {
int i = low, j = high;
int pivot = numbers[low + (high-low)/2];
while (i <= j) {
while (numbers[i] < pivot) {
i++;
}
while (numbers[j] > pivot) {
j--;
}
if (i <= j) {
swap(i, j);
i++;
j--;
}
}
if (low < j)
quicksort(low, j);
if (i < high)
quicksort(i, high);
}
private void swap(int i, int j) {
int temp = numbers[i];
numbers[i] = numbers[j];
numbers[j] = temp;
}
public static void main(String[] args) {
int[] array = {1,3,2,4};
QuickSort2 tool = new QuickSort2();
tool.sort(array);
for( int i = 0; i < array.length; i++){
System.out.println(array[i]);
}
}
}
选择排序
package sort;
public class selectSort {
public static void main(String[] args) {
int a[] = {1,54,6,3,78,34,12,45};
int position = 0;
for(int i = 0; i < a.length; i++){
int j = i + 1;
position = i;
int temp = a[i];
for(; j < a.length; j++){
if( a[j] < temp){
temp = a[j];
position = j;
}
}
a[position] = a[i];
a[i] = temp;
}
for(int i = 0;i < a.length;i++)
System.out.println(a[i]);
}
}
希尔排序
package sort;
public class shilSort {
public static void main(String[] args) {
int a[] = {1,54,6,3,78,34,12,45,56,100};
double d1 = a.length;
int temp = 0;
System.out.println("begin...");
while(true) {
d1 = Math.ceil( d1/2 );
int d = (int) d1;
for(int x = 0; x < d; x++){
for( int i = x + d; i < a.length; i += d){
int j = i - d;
temp = a[i];
for(; j >= 0 && temp < a[j]; j -= d){
a[j+d] = a[j];
}
System.out.println("Jerry insert value: " + temp +
" to Position: < " + (j+d) + " >");
a[j+d] = temp;
}
}
if( d == 1){
break;
}
}
for(int i = 0; i < a.length;i++){
System.out.println(a[i]);
}
}
}