Summary of common sorting algorithms (implemented in java)

Source: Internet
Author: User

Sorting is an operation that sorts a string of records in ascending or descending order based on the size of one or more keywords. Common sorting algorithms include selection sorting, insertion sorting, Hill sorting, Merge Sorting, and quick sorting.

Since comparison and exchange are inevitable in the sorting process, we extract them into two separate functions, as shown below:

// For the versatility of the sorting code, it is assumed that the elements to be sorted implement the Comparable interface private static boolean less (Comparable v, Comparable w) {return v. compareTo (w) <0;} private static void exch (Comparable [] a, int I, int j) {Comparable t = a [I]; a [I] = a [j]; a [j] = t ;}

Select sort

Algorithm idea: First, find the smallest element in the array, and then swap it with the first element in the array. Find the smallest element from the remaining element, and switch it to the second element in the array, so that until the entire array is fully ordered.

Algorithm features: 1. the running time is irrelevant to the input. Whether the input data is an ordered array or a random array, the number of comparisons is the same.

2. The number of rows in the array is the least. Because each time a minimum element is selected and its location is exchanged, an element can be scheduled for each exchange, so the total number of exchanges is N.

Public static void selectionSort (Comparable [] a) {int N =. length; for (int I = 0; I <N; I ++) {int min = I; // assume that the first number is the smallest for (int j = I + 1; j <N; j ++) if (less (a [j], a [min]) min = j; // obtain the subscript exch (a, I, min); // Switch location }}
Insert sort

Algorithm idea: first, assume that the first element in the array is ordered, and then insert the second element into the appropriate position of the preceding ordered array to form a new ordered array, then insert the third element into the preceding ordered array, so that you know the overall order of the array.

Algorithm features: the running time is related to the initial input. If the initial input data has been sorted or partially ordered, the insertion sorting performance will be better.

Public static void insertionSort (Comparable [] a) {int N =. length; for (int I = 1; I <N; I ++) {// subscript of the element to be inserted for (int j = I; j> 0; j --) // find the proper Insert Location in the preceding ordered array if (less (a [j], a [J-1]) exch (a, j, J-1 );}}
Hill sorting

Algorithm idea: the logic of hill sorting is to make the elements with any interval of h in the array orderly. Such an array is called an h-ordered array. We continue to reduce the value of h to make it eventually become a 1-ordered array. 1-ordered array, that is, the overall order of the array. This sorting is complete.

The most difficult part of hill sorting is the selection of h series. There is no way to prove that a sequence is the best.

Public static void sort (Comparable [] a) {int N =. length; int h = 1; // used to record the step while (h <N/3) h = 3 * h + 1; // The step size is 1, 4, 13, 40 ...... While (h> = 1) {for (int I = h; I <N; I ++) for (int j = I; j> = h; j-= h) // insert a [I] into a [I-h], a [I-2 * h], a [I-3 * h]... If (less (a [j], a [j-h]) exch (a, j, j-h); h = h/3; // reverse the step of the previous record to make it a 1-ordered array }}
Merge Sorting

Algorithm idea: to sort an array, You Can recursively divide it into two halves for sorting, and finally combine the results to make the overall order. Merge Sorting is based on the Division idea in algorithm design. We break down a big problem into small problems to solve them separately, and then use the answers to all small problems to solve the whole big problem.

Algorithm features: 1. The time required for sorting arrays of any length of N is proportional to NlogN. This can be obtained through the sort tree of Merge Sorting.

2. The extra space required is proportional to N. This is because an auxiliary array with a length of N is required during the merge process.

Private static Comparable [] temp; // auxiliary array, defined as the class member variable public static void mergeSort (Comparable [] a) {temp = new Comparable [. length]; sort (a, 0,. length-1);} // returns the array a [lo .. hi] sort private static void sort (Comparable [] a, int lo, int hi) {if (lo> = hi) return; int mid = lo + (hi-lo) /2; sort (a, lo, mid); // recursively sorts sort (a, mid + 1, hi) on the left ); // recursively sort merge (a, lo, mid, hi) on the Right; // merge the left and right sides} // combine two ordered arrays a [lo .. mid] And a [mid + 1 .. hi] merges into an ordered array and uses an auxiliary array temppublic static void merge (Comparable [] a, int lo, int mid, int hi) {int I = lo, j = mid + 1; for (int k = lo; k <= hi; k ++) // copy array a to the auxiliary array temp. temp [k] = a [k]; for (int k = lo; k <= hi; k ++) if (I> mid) a [k] = temp [j ++]; // The left half side is exhausted, directly copy the remaining elements of the right half to the array. else if (j> hi) a [k] = temp [I ++]; // the right half side is exhausted, directly copy the remaining elements in the left half to the array. else if (less (temp [j], temp [I]) a [k] = temp [j ++]; // The current element of the right half side is smaller than the current element of the left half side. Take the right half side elsea [k] = temp [I ++]; // The current element of the left half side is smaller than the current element of the right half side, and the left half side is taken}
Quick sorting

Algorithm idea: Fast sorting is a Sort Algorithm Based on grouping. It divides an array into two word groups and sorts them separately.

Public static void sort (Comparable [] a) {sort (a, 0,. length-1);} private static void sort (Comparable [] a, int lo, int hi) {if (hi <= lo) return; int j = partition (, lo, hi); // Split sort (a, lo, J-1); // sort (a, j + 1, hi) in the left half ); // sorting in the right half} private static int partition (Comparable [] a, int lo, int hi) {int I = lo, j = hi + 1; // left and right scan pointer Comparable v = a [lo]; // split element. After splitting, the position is fixed and while (true) is returned) {// round-robin scanning pointer while (less (a [++ I], v) if (I = hi) break; while (less (v, a [-- j]) if (j = lo) break; if (I> = j) break; exch (a, I, j);} exch (, lo, j); // put v = a [j] into the correct position: return j; // return the position of the split element}

In the file to be sorted, if multiple records with the same keywords exist, the relative sequence of these records with the same keywords remains unchanged after sorting, and the sorting method is stable; if the relative order between records with the same keywords changes, this sorting method is not stable.


Sorting Method

Average time

Worst case

Stability

Extra space

Remarks

Select

O (n2)

O (n2)

Unstable

O (1)

N hours is better

Insert

O (n2)

O (n2)

Stability

O (1)

Most of the sorted items are better

Shell

O (nlogn)

O (ns) 1 <s <2

Unstable

O (1)

S is the selected group

Fast

O (nlogn)

O (n2)

Unstable

O (nlogn)

N is better

Merge

O (nlogn)

O (nlogn)

Stability

O (1)

N is better


This algorithm only focuses on implementation and does not use stacks or other mechanisms to eliminate recursion,

Complete procedure: http://download.csdn.net/detail/danchu/7276229

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.