# Sorting

guoluona

Bubble Sort就不说了，下面简单总结一个Selection Sort, Insertion Sort, Merge Sort和Quick Sort:

1.Selection Sort:

(29, 64, 73, 34, 20)
20, (64, 73, 34, 29)
20, 29, (73, 34, 64)
20, 29, 34, (73, 64)
20, 29, 34, 64, (73)

``````public class SelectionSort {
public static void selectionSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int min = i;
for (int j = i + 1; j < arr.length; j++) {
min = arr[j] < arr[min] ? j : min;
}
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}

public static void main(String[] args) {
int[] arr = {5, 32, 23, 5, 6, 8, 44};
selectionSort(arr);
for (int num : arr) {
System.out.print(num + " ");
}
}
}``````

2.Insertion Sort:

29, 20, 73, 34, 64
(29), 20, 73, 34, 64
(20, 29), 73, 34, 64
(20, 29, 73), 34, 64
(20, 29, 34, 73), 64
(20, 29, 34, 64, 73)

``````public class InsertionSort {
public static void InsertionSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int index = arr[i], j = i;
while (j > 0 && arr[j - 1] > index) {
arr[j] = arr[j - 1];
j--;
}
arr[j] = index;
}
}

public static void main(String[] args) {
int[] arr = {5, 32, 23, 5, 6, 8, 44};
InsertionSort(arr);
for (int num : arr) {
System.out.print(num + " ");
}
}
}``````

3.Merge Sort:

``````public class MergeSort {
public static int[] mergeSort(int[] arr) {
if (arr == null || arr.length == 0 || arr.length == 1) return arr;
int mid = arr.length / 2;
int[] left = mergeSort(Arrays.copyOfRange(arr, 0, mid));
int[] right = mergeSort(Arrays.copyOfRange(arr, mid, arr.length));

return merge(left, right);

}

public static int[] merge(int[] left, int[] right) {
int[] result = new int[left.length + right.length];
int i = 0, j = 0, k = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j]) {
result[k++] = left[i++];
} else {
result[k++] = right[j++];
}
}
while (i < left.length) {
result[k++] = left[i++];
}
while (j < right.length) {
result[k++] = right[j++];
}

return result;
}

public static void main(String[] args) {
int[] arr = {5, 32, 23, 5, 6, 8, 44};
int[] result = mergeSort(arr);
for (int num : result) {
System.out.print(num + " ");
}
}
}
``````

4.Quick Sort:

``````public class QuickSort {
public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

public static void quickSort(int[] arr, int low, int high) {
if (arr == null || arr.length == 0) return;
if (low >= high) return;

int mid = low + (high - low) / 2;
int pivot = arr[mid];

int i = low, j = high - 1;
while (i <= j) {
while (arr[i] < pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i <= j) {
swap(arr, i, j);
i++;
j--;
}
}
if (low < j) {
quickSort(arr, low, j);
}
if (i < high) {
quickSort(arr, i, high);
}
}

public static void main(String[] args) {
int[] arr = {5, 32, 23, 5, 6, 8, 44};
quickSort(arr, 0, arr.length - 1);
for (int num : arr) {
System.out.print(num + " ");
}
}
}``````

5.Heap Sort

``````public class HeapSort {
public static int N;
//Build a heap
public static void heapify(int[] arr) {
N = arr.length - 1;
//Bottom up, 也只能bottomup，由上往下的话，根结点有时候会不是最优解
for (int i = N / 2; i >= 0; i--) {
maxHeap(arr, i);
}
}
//swap the largest element to root
public static void maxHeap(int[] arr, int i) {
int left = 2 * i;
int right = 2 * i + 1;
int max = i;
if (left <= N && arr[left] > arr[i]) {
max = left;
}
if (right <= N && arr[right] > arr[max]) {
max = right;
}
if (max != i) {
swap(arr, i, max);
maxHeap(arr, max);
}
}

public static void swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

public static void heapSort(int[] arr) {
heapify(arr);
for (int i = N; i > 0; i--) {
swap(arr, 0, i);
N = N - 1;
maxHeap(arr, 0);
}

}

public static void main(String[] args) {
int[] arr = {5, 32, 23, 5, 6, 8, 44};
heapSort(arr);
for (int num : arr) {
System.out.print(num + " ");
}
}
}``````

199 声望
14 粉丝
0 条评论