Common sorting algorithms

Source: Internet
Author: User

1. Select sort (Time complexity O (n2))

The idea of sorting is to find the minimum number in the linear table, place it in the table header, and then find the minimum number in the remaining number, placed after the first number, until only one number is left in the linear table.

Java implementations:

public static void Choicesort (integer[] a) {if (a = = NULL | | a.length <= 0) {return;} for (int i = 0; i < a.length; i++) {int min = i;/* Defines the current subscript as the minimum subscript */for (int j = i + 1; j < A.length; J + +) {if (A[mi N] > A[j]) {/* If there is a keyword less than the current minimum value */min = j;/* Assigns the subscript of this keyword to min */}}if (i! = min) {/* if min is not equal to I, the description finds the minimum value, exchange */int TMP = A[min ];a[min] = a[i];a[i] = tmp;}}}

  

2. Insert sort (time complexity is O (n2))

The idea of inserting a sort is to repeatedly insert a new element in an already lined subarray until the entire array is fully sequenced.

Java implementations:

public static void Insertsort (int[] arr) {for (int i = 1; i < arr.length; i++) {if (arr[i-1] > Arr[i]) {int temp = arr[i];//the element to be inserted int j = i;while (J > 0 && arr[j-1] > Temp) {//will move back one arr[j] greater than temp = arr[j-1];j--;} ARR[J] = temp;}}}

  

3. Bubble sort (Time complexity O (n2))

The idea of bubbling sorting is to compare successive contiguous elements in each traversal, and if a pair of elements are descending, swap their values; otherwise they will remain unchanged. After the first iteration, the last element becomes the maximum number in the array. After the second traversal, the second-to-last element becomes the second largest number in the array. The entire process continues until all elements have been sorted.

Note: If no transformations occur in a traversal, then the next traversal is not necessary because all elements are already sorted.

Java implementations:

public static void Bubblesort (int[] list) {Boolean neednextpass = true;for (int k = 1; k < list.length && Neednex Tpass; k++) {neednextpass = false;for (int i = 0; i < list.length-k; i++) {if (List[i] > list[i+1]) {int temp = List[i];list[i ] = list[i + 1];list[i + 1] = Temp;neednextpass = True;}}}

  

4. Fast sequencing (Time complexity O (NLOGN))

The idea of a quick sort is to select an element of a primitive in the array, dividing the array into two parts so that all the elements in the first part are less than or equal to the principal, and all the elements in the second part are larger than the primary. Apply fast sorting algorithm recursively to the first part, then apply fast sorting algorithm recursively to the second part.

Java implementations:

public static void QuickSort (int[] list) {quickSort (list, 0, list.length-1);} public static void QuickSort (int[] list, int first, int. last) {if (last > first) {int pivotindex = partition (list, Firs T, last); QuickSort (list, first, pivotIndex-1), QuickSort (list, Pivotindex + 1, last);}} private static int partition (int[] list, int first, int last) {int pivot = list[first];int Low = first + 1;int high = Last while (High > Low) {while (Low <= high && List[low] <= pivot) low++;while (Low <= high && list [High] > Pivot) high--;if (High > low) {int temp = List[high];list[high] = List[low];list[low] = temp;}} while (High > First && list[high] >= pivot) high--;if (Pivot > List[high]) {List[first] = list[high];list[ High] = Pivot;return high;} else {return first;}}

  

5. Merge sort (time complexity O (NLOGN))

The idea of merge sort is to divide the array into two halves and apply the merge sort recursively to each part. After all two parts have been sequenced, they are merged.

Java implementations:

public static void MergeSort (int[] list) {if (List.length > 1) {int[] firsthalf = new INT[LIST.LENGTH/2]; System.arraycopy (list, 0, firsthalf, 0, LIST.LENGTH/2); MergeSort (firsthalf); int secondhalflength = LIST.LENGTH-LIST.L ength/2;int[] secondhalf = new Int[secondhalflength]; System.arraycopy (list, LIST.LENGTH/2, Secondhalf, 0,secondhalflength); MergeSort (secondhalf); int[] temp = merge ( Firsthalf, secondhalf); System.arraycopy (temp, 0, list, 0, temp.length);}} public static int[] Merge (int[] List1, int[] list2) {int[] temp = new Int[list1.length + list2.length];int pos1 = 0;int PO S2 = 0;int Pos3 = 0;while (pos1 < list1.length && Pos2 < list2.length) {if (LIST1[POS1] < LIST2[POS2]) tem p[pos3++] = list1[pos1++];elsetemp[pos3++] = list2[pos2++];} while (Pos1 < list1.length) temp[pos3++] = List1[pos1++];while (Pos2 < list2.length) temp[pos3++] = list2[pos2++]; return temp;}

  

Common sorting algorithms

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.