5 sort algorithms and 5 sort algorithms are summarized.

Source: Internet
Author: User

5 sort algorithms and 5 sort algorithms are summarized.

1 Overview

This article summarizes and parses commonly used and efficient sorting algorithms, and provides simplified implementation code, including sorting by selection, insert sorting, Merge Sorting, Hill sorting, and quick sorting. Shows the algorithm performance comparison:

 

2 select sorting

The first round of sorting is to select the smallest data from all n data records in the data sequence as the 1st elements in the sequence and locate it at the first storage location, the second round of processing selects a second small element from the n-1 data in the data sequence as the 2nd elements in the sequence, positions it in the second storage location, and so on, when n-1 is processed, a small element is selected from the remaining two elements of the data sequence as the last 2nd elements in the sequence and located at the second-to-last storage location, so far, the entire sorting process has been completed.

The Code is as follows:

Public class SelectionSort {public void selectionSort (int [] array) {int temp; for (int I = 0; I <array. length-1; I ++) {for (int j = I + 1; j <= array. length-1; j ++) {// comparison between nth I and NTH j can get the last bit, so use j <= array. length-1 if (array [I]> array [j]) {// note the difference with Bubble sorting. Here we compare it with j. Temp = array [I]; array [I] = array [j]; array [j] = temp ;}// print the sorting result for (int m = 0; m <= array. length-1; m ++) {System. out. print (array [m] + "\ t");} System. out. println () ;}} public static void main (String [] args) {SelectionSort selectionSort = new SelectionSort (); int [] array = {5, 69, 12, 3, 56,789, 2, 5648, 23}; selectionSort. selectionSort (array); for (int m = 0; m <= array. length-1; m ++) {System. out. print (array [m] + "\ t ");}}}

 

3 insert sort

The sorting principle of direct insertion sorting is to arrange a group of unordered numbers in a row. The first number on the left is the number that has been sorted, and the other number is the number that has not been sorted. Then, unordered numbers are inserted to sorted numbers from left to right.

The Code is as follows:

Public class InsertSort {public void insertSort (int [] array, int first, int last) {int temp, I, j; for (I = first + 1; I <= last-1; I ++) {// by default, the first number is an ordered sequence, followed by the number to be inserted. Temp = array [I]; j = I-1; while (j> = first & array [j]> temp) {// search from the backend. If the number of searched results is smaller than temp, the search continues, until the number of values less than or equal to temp is found, array [j + 1] = array [j]; j --;} array [j + 1] = temp; // print the result of each sort for (int m = 0; m <= array. length-1; m ++) {System. out. print (array [m] + "\ t");} System. out. println () ;}} public static void main (String [] args) {InsertSort insertSort = new InsertSort (); int [] array = {5, 69, 12, 3, 56,789, 2, 5648, 23}; insertSort. insertSort (array, 0, array. length); // note that the value is 0-9 instead of 0-8 for (int I = 0; I <= array. length-1; I ++) {System. out. print (array [I] + "\ t ");}}}

4. Merge and sort

Algorithm Description:

Divide the sequence into two halves that are as equal as possible.

Sort the two half elements separately.

Merge two ordered tables into one.

The Code is as follows:

Public class MergeSortTest {public void sort (int [] array, int left, int right) {if (left> = right) return; // find the intermediate index int center = (left + right)/2; // recursive sort (array, left, center) on the left array ); // recursive sort (array, center + 1, right) on the right side of the array; // merge (array, left, center, right ); // print the result of each sort for (int I = 0; I <array. length; I ++) {System. out. print (array [I] + "\ t");} System. out. println ();}/*** merges the two arrays and merges the first two arrays in order, after merging, it is still ordered ** @ param array * array object * @ param left * index of the first element of the left array * @ param center * index of the last element of the left array, center + 1 is the index of the first element of the right array * @ param right * index of the last element of the right array */public void merge (int [] array, int left, int center, int right) {// temporary array int [] tmpArr = new int [array. length]; // index of the first element in the right array int mid = center + 1; // index of the temporary array of third records int third = left; // cache the index of the first element in the left array, int tmp = left; while (left <= center & mid <= right) {// fetch the smallest value from the two arrays into the temporary array if (array [left] <= array [mid]) {tmpArr [third ++] = array [left ++];} else {tmpArr [third ++] = array [mid ++];} // Add the remaining parts to the temporary array in sequence (in fact, only one of the two while are executed) while (mid <= right) {tmpArr [third ++] = array [mid ++];} while (left <= center) {tmpArr [third ++] = array [left ++];} // copy the content in the temporary array to the original array // (the content in the original left-right range is copied back to the original array) while (tmp <= right) {array [tmp] = tmpArr [tmp ++];} public static void main (String [] args) {int [] array = new int [] {5, 69, 12, 3, 56,789, 2, 5648, 23}; MergeSortTest mergeSortTest = new MergeSortTest (); mergeSortTest. sort (array, 0, array. length-1); System. out. println ("sorted array:"); for (int m = 0; m <= array. length-1; m ++) {System. out. print (array [m] + "\ t ");}}}

 5. Hill sorting

The basic idea of this method is as follows: first, the entire sequence of elements to be sorted is divided into several sub-sequences (composed of elements separated by a certain "increment") for direct insertion and sorting, and then the incremental sorting is reduced in turn, when the elements in the entire sequence are basically ordered (the increment is small enough), all the elements are directly inserted for sorting. Because the efficiency of direct insert sorting is very high when the elements are basically ordered (close to the best condition), the time efficiency of hill sorting is much higher than that of the first two methods.

The Code is as follows:

Public class ShellSort {public void shellSort (int [] array, int n) {int I, j, gap; int temp; for (gap = n/2; gap> 0; gap/= 2) {// calculate the gap size for (I = gap; I <n; I ++) {// group the data for (j = I-gap; j> = 0 & array [j]> array [j + gap]; j-= gap) {// sort data in each group. temp = array [j]; array [j] = array [j + gap]; array [j + gap] = temp;} // print the sorting result for (int m = 0; m <= array. length-1; m ++) {System. out. print (array [m] + "\ t");} System. out. println () ;}} public static void main (String [] args) {ShellSort shellSort = new ShellSort (); int [] array = {5, 69, 12, 3, 56,789, 2, 5648, 23}; shellSort. shellSort (array, array. length); // note that the number of arrays is for (int m = 0; m <= array. length-1; m ++) {System. out. print (array [m] + "\ t ");}}}

 6. Quick sorting

Quicksort is an improvement in Bubble sorting. Proposed by C. A. R. Hoare in 1962. Its basic idea is: Split the data to be sorted into two independent parts by one sort, and all the data in one part is smaller than all the data in the other part, then, sort the two data parts by using this method. The entire sorting process can be recursive to convert the entire data into an ordered sequence.

The Code is as follows:

Public class QuickSort {public int partition (int [] sortArray, int low, int height) {int key = sortArray [low]; // At the beginning, the first number indicates the data while (low 

 

Only some sorting algorithms summarized by myself are posted here. For more algorithm summaries, refer to your own article: How to Learn Java

 

You are welcome to repost original articles. Please indicate the source when reprinting.

  Recommended articles by the author:

Java self-learning approach

Deploy Hadoop2.3.0 in Eclipse and submit mapreduce tasks directly in Eclipse

Java: how to obtain system information (including operating system, jvm, cpu, memory, hard disk, and network)

Java instructions on how to generate a QR code

 


For comparison of sorting algorithms, select five sorting algorithms to complete sorting comparison results, including calculation time, etc.

Sorting Algorithm
<Script>
Array. prototype. swap = function (I, j)
{
Var temp = this [I];
This [I] = this [j];
This [j] = temp;
}

Array. prototype. bubbleSort = function ()
{
For (var I = this. length-1; I> 0; -- I)
{
For (var j = 0; j <I; ++ j)
{
If (this [j]> this [j + 1]) this. swap (j, j + 1 );
}
}
}

Array. prototype. selectionSort = function ()
{
For (var I = 0; I <this. length; ++ I)
{
Var index = I;
For (var j = I + 1; j <this. length; ++ j)
{
If (this [j] <this [index]) index = j;
}
This. swap (I, index );
}
}

Array. prototype. insertionSort = function ()
{
For (var I = 1; I <this. length; ++ I)
{
Var j = I, value = this [I];
While (j> 0 & this [j-1]> value)
{
This [j] = this [j-1];
-- J;
}
This [j] = value;
}
}

Array. prototype. shellSort = function ()
{
For (var step = this. length> 1; step> 0; step> = 1)
{
For (var I = 0; I <step; ++ I)
{
For (var j = I + step; j <this. length; j + = step)
{
Var k = j, value = this [j];
While (k> = step & this [k-step]> value)
... The remaining full text>

Comprehensive Functions of sorting algorithms: random generation of data; implementation of five common sorting algorithms; time-based analysis efficiency and comparison (c)

Bubble, select, merge, fast sorting, and heap sorting ..

Related Article

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.