Java Implementation of the seven sorting algorithms (bubble, select, insert, Hill, fast, merge, and heap sorting)

Source: Internet
Author: User

Bubble Sorting: Put the largest or smallest element at the end of the array each time. so easy! Time Complexity: (O (n ^ 2 ))

public class BubbleSort {public static void bubbleSort(int[] a) {for (int j = 1; j < a.length; j++) {for (int i = 0; i < a.length - j; i++) {if (a[i] > a[i + 1]) {int temp = a[i];a[i] = a[i + 1];a[i + 1] = temp;}}}}public static void main(String[] args) {int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7,3, 8, 31 };bubbleSort(a);for(int i = 0; i < a.length; i++){System.out.print(a[i]+" ");}}}

Select the sorting method: similar to bubble, each time the smallest element is placed in front. Time Complexity: (O (n ^ 2 )))
public class SelectSort {public static void selectSort(int[] a) {int min;for (int i = 0; i < a.length - 1; i++) {min = i;for (int j = min + 1; j < a.length; j++) {if (a[min] > a[j]) {int temp = a[min];a[min] = a[j];a[j] = temp;}}}}public static void main(String[] args) {int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7,2, 3, 0, 43, 23, 12, 4, 1, 15, 7, 3, 8, 31 };selectSort(a);for (int i = 0; i < a.length; i++) {System.out.print(a[i] + " ");}}}

Insert sorting logic: Start from the second element and compare it with the elements in the sorted Word Group to find a proper position, and then move the following elements one by one, insert the element to the appropriate position. Time Complexity: (O (n ^ 2 ))
public class InsertSort {public static void insertSort(int[] a) {for (int i = 1; i < a.length; i++) {int key = a[i];int j = i - 1;while (j >= 0 && a[j] > key) {a[j+1] = a[j];j--;}a[j+1] = key;}}public static void main(String[] args) {int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7,2, 3, 0, 43, 23, 12, 4, 1, 15, 7, 3, 8, 31 };insertSort(a);for (int i = 0; i < a.length; i++) {System.out.print(a[i] + " ");}}}

Hill sorting method: similar to insert sorting, The step size is (the length of the array/2/I ). The time complexity is (n * log n ).
public class ShellSort {public static void shellSort(int[] a) {for (int gap = a.length / 2; gap > 0; gap /= 2)for (int i = gap; i < a.length; i++) {int key = a[i];int j = i - gap;while (j >= 0 && a[j] > key) {a[j + gap] = a[j];j -= gap;}a[j + gap] = key;}}public static void main(String[] args) {int a[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7,2, 3, 0, 43, 23, 12, 4, 1, 15, 7, 3, 8, 31 };shellSort(a);for (int i = 0; i < a.length; i++) {System.out.print(a[i] + " ");}}}

Fast sorting: The key lies in finding the partition () function. I have provided two methods: 1. from front to back. 2. From the front to the middle, from the back to the middle. The time complexity is (n * log n). The worst case is OK! Show your my codes!
Public class QuickSort {/* public static int partition (int [] a, int p, int r) {int x = a [r]; int I = p-1; for (int j = p; j <r; j ++) {if (a [j] <= x) {// if a [j]
 
  
Merge and sort ideas: Use the divide and conquer idea to divide the array to the minimum, and then merge it. Don't know how to google it! The time complexity (n * log n) is a stable sorting algorithm.
public class MergeSort {public static void merge(int A[], int p, int q, int r) {int[] L = new int[q - p + 1];int[] R = new int[r - q];for (int i = p, j = 0; i <= q; i++, j++) {L[j] = A[i];}for (int i = q + 1, j = 0; i <= r; i++, j++) {R[j] = A[i];}int pos = p;int i = 0, j = 0;for (; i < L.length && j < R.length;) {if (L[i] <= R[j]) {A[pos++] = L[i++];} else {A[pos++] = R[j++];}}if (i < L.length) {for (; i < L.length;)A[pos++] = L[i++];} else if (j < R.length) {for (; j < R.length;)A[pos++] = R[j++];}}public static void mergeSort(int[] A, int p, int r) {if (p < r) {int q = (p + r) / 2;mergeSort(A, p, q);mergeSort(A, q + 1, r);merge(A, p, q, r);}}public static void main(String[] args) {int A[] = { 5, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 2, 45, 7, 2, 4, 23, 7, 2, 3, 0, 43, 23, 12, 4, 1, 15, 7,3, 8, 31 };mergeSort(A, 0, A.length - 1);for (int i = 0; i < A.length; i++) {System.out.print(A[i] + " ");}}}

Heap sorting: You can create a heap, either a large or a small heap. Each time the top element of the heap is placed at the end of the array, the size of the heap is reduced by one, and the nature of the top heap of the first element is maintained. The time complexity is (n * log n ).
Public class HeapSort {// obtain the location of the parent node static int parent (int I) {return I/2;} // obtain the location of the left child static int left (int I) {return 2 * I;} // locate the right child location static int right (int I) {return 2 * I + 1 ;} // maintain the nature of the Large top heap static void maxHelpify (int [] A, int I) {int l = left (I); int r = right (I ); int largest = 0; if (l <= A [0] & A [l]> A [I]) largest = l; elselargest = I; if (r <= A [0] & A [r]> A [largest]) largest = r; if (largest! = I) {int temp = A [largest]; A [largest] = A [I]; A [I] = temp; maxHelpify (A, largest );}} // create A large top heap static void buildMaxHeap (int [] A) {for (int I = (. length-1)/2; I> 0; I --) {maxHelpify (A, I) ;}// heap sorting public static void heapSort (int []) {buildMaxHeap (A); // create A large top heap. // put the number of top heap points at the end of the array, and then reduce the heap size by 1, retained for (int I =. length-1; I> = 2; I --) {int temp = A [1]; A [1] = A [I]; A [I] = temp; A [0] --; maxHelpify (A, 1) ;}} public static void main (String [] args) {int A [] = new int [3]; A [0] =. length-1; // a [0] storage heap size for (int I = 1; I <. length; I ++) {A [I] = (int) (Math. random () * 10);} heapSort (A); for (int I = 1; I <. length; I ++) {System. out. print (A [I] + "");}}}

Others: there is also a sort of counts.

Relatively simple: the array subscript is used as the value of the element, which is used in special cases. For example, the age of N people can be sorted by count. Consider age as the subscript of the array, define an array of 100 in size, compare age with it, If age = subscript will, the value of this submark + 1. the time complexity is (N ).





















Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.