Summary of sorting algorithms

Source: Internet
Author: User

Code for a long time, more and more feel that the return to the later is the basis. Suddenly feel regret freshman sophomore did not learn these basic computer courses, the loss of large.
Summarize the sorting algorithm:
package sorting algorithm;/** * 1. Select sort * 2. Insert sort * 3. Merge sort * 4. Quick Sort * * @author Administrator * */public class four sort algorithm {public static void Main (string[] args) {int[] arr = {2, 5, 6, 7, 4, 3, 1, 1, 3, 4, 5, 1, 3, 6, 3};//selectsort (arr);//Insertedsort ( ARR);//MergeSort (arr, 0, arr.length-1); QuickSort (arr, 0, arr.length-1); out (arr);} Output public static void out (int[] arr) {for (int e:arr) System.out.print (E + ",");}  1. Select sort public static void Selectsort (int[] a) {for (int i = 0; i < a.length; i++) for (int j = i + 1; j < A.length; J + +) {int key = A[i];if (Key > A[j]) {a[i] = a[j];a[j] = key;}}} 2. Insert sort public static void Insertedsort (int[] a) {for (int i = 1; i < a.length; i++) {int key = A[i];int J = I-1;whi Le (j >= 0 && key < A[j]) {a[j + 1] = a[j];a[j] = key;j--;}}} 3. Merge sort (partition method) public static void MergeSort (Int[] A, int. low, int.) {if (Low < high) {int mid = (low + high)/2;// Recursion to the smallest atom mergesort (A, low, mid), MergeSort (A, mid + 1, high);//merge array Mergearray (a, low, Mid, High);}} Merge array private static void Mergearray (int[] A, int low, int mid, Int. high) {int N = high-low;int[] Temp = new Int[n + 1] ; int i = low, J = mid + 1;int m = Mid, n = high;int k = 0;while (i <= m && j <= N) temp[k++] = (A[i] < a[j ] ?  A[i++]: a[j++]); while (I <= m) temp[k++] = A[i++];while (j <= N) temp[k++] = a[j++];for (i = 0; i < K; i++) A[low + I] = temp[i];} 4. Quick sort (partition method) public static void QuickSort (Int[] A, int. low, int high) {if (Low < high) {int r = Adjustarray (A, Low, hi GH); QuickSort (A, low, r-1); QuickSort (A, R + 1, high);}} Extracts the first element in an array and splits it. The small element is placed on the left side of the <strong> logo </strong>, the element with a greater than//flag is placed on its right, and finally returns the position of the flag element private static int Adjustarray (int[] A, int low, int high) {int i = low, j = high;int Spcvalue = A[low];while (i < J) {while (A[j] >= spcvalue && i < J) J -;if (A[j] < spcvalue) a[i++] = A[j];while (A[i] <= spcvalue && i < j) I++;if (A[i] > Spcvalue) a[j--] = A[i];} A[i] = Spcvalue;return i;}}


Summary of 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.