Eight sorting algorithms in the Data Structure

Source: Internet
Author: User

Eight sorting algorithms in the Data Structure

I. Bubble Sorting

Thought: I repeatedly visited the sequence to be sorted and compared two elements at a time. If their order is wrong, I will exchange them. The smallest elements come at a time, followed by the second smallest.

Time Complexity: O (n ^ 2)

Space complexity: O (1)

Stability: Stability

1.

 

/* ** Bubble sort * @ param disOrderArray * @ return */public static int [] BubbleSort (int [] disOrderArray) {int temp; // The first loop: indicates the number of comparisons, such as length elements. The number of comparisons is length-1 (certainly not compared with yourself) for (int I = 0; I
 
  
I; j --) {// here is <when its return is sorted from small to large,> when its return is from large to small if (disOrderArray [j] <disOrderArray [J-1]) {temp = disOrderArray [j]; disOrderArray [j] = disOrderArray [J-1]; disOrderArray [J-1] = temp ;}} return disOrderArray ;}
 

Ii. Quick sorting

 

Thought: sort the records to be sorted into two parts. If some of the record keywords are smaller than the keywords of the other part of the record, you can sort these two keywords separately, to order the entire sequence.

Time Complexity: O (nlogn). The worst case is O (n ^ 2)

Space complexity: O (1)

Stability: unstable

 

/*** Fast sorting ** thought: * sort the records to be sorted into two separate parts. the keywords of one part of the records are smaller than those of the other, * You can sort these two records separately to achieve the goal of sequence order. ** the essence is to find a base position (pivot, watershed, the function is that the left side is smaller than it, and the right side is bigger than it. it can be randomly named base *. First, we start from the rightmost side of the sequence and find the value smaller than base *. If the value is small, we change the position and move base to the right side (smaller than base in comparison) in this way, the right side of the base is larger than the base *. Then, start from the leftmost side of the sequence to find the * larger than the base. If it is larger, change the position, so that the base is moved to the left (larger than the base at the time of comparison) (recorded as a temporary low bit), so that the left side of the base is smaller than the base ** loop over the two steps above, it was not until low = heigh that the pivot and watershed were found. returns the left and right of the watershed, and returns the recursion */public static int [] quickSort (int [] arr, int low, int heigh) {if (low 

3. Directly select sorting:

 

Thought: The smallest (or largest) value will be selected for each sort, and the order will be placed behind the sorted Series

Time Complexity: O (n ^ 2)

Space complexity: O (1)

Stability: unstable

 

/* ** Directly select sorting * directly select sorting the smallest value for each trip * @ param arr * @ return */public static int [] selectionSort (int [] arr) {for (int I = 0; I arr [j]) {int temp = arr [j]; arr [j] = arr [I]; arr [I] = temp; }}return arr ;}

Iv. Heap sorting

 

Idea: heap sorting is a sort algorithm designed by using the data structure such as heap. It can quickly locate the elements of a specified index using the characteristics of arrays.

Time Complexity: O (nlogn)

Space complexity: O (1)

Stability: unstable

 

/*** Heapsort refers to a sort algorithm designed by using the data structure such as heap. It can quickly locate the elements of a specified index using the features of arrays. * @ Param arr * @ return */public static int [] heapSort (int [] arr) {int I; // create arr into a large top heap // from 0 to arr. length/2. These are all nodes with children. // it makes no sense to construct a big top heap for (I = arr. length/2; I> = 0; I --) {heapAdjust (arr, I, arr. length-1);} for (I = arr. length-1; I> 0; I --) {swap (arr, 0, I); // set arr [0... i-1] reconstructs into a big top heapAdjust (arr, 0, I-1);} return arr;} private static void heapAdjust (int [] arr, int s, int m) {int temp, j; temp = arr [s]; // point to the temporary (relative to the root node) root Node for (j = 2 * s; j <= m; j * = 2) {// if the right node is larger than the left node, move the current node to the right node if (j <m & arr [j] <arr [j + 1]) {// point to the right node j ++;} // The current parent node is greater than the current node. // No need to process it if (temp> = arr [j]) {break;} // The current parent node is smaller than the child node under it. // replace the child node with the parent node. // if it is a leaf node, then it should be the smallest (relative to its ancestors) // The purpose of this method is to exchange the values of parent and children, construct a large root heap // run it here to indicate the parent node of the current node (the temporary root node is smaller than the current node), // Move the current node to the top, changing the location // It doesn't matter if arr [s] is overwritten, because temp records this value (original root node (relative parent) arr [s] = arr [j]; // currently, this element is regarded as a temporary parent node. // to find the child node of this element, check if there is any element larger than the current value. // The Last s points to the element s = j;} arr [s] = temp ;}


5. Insert sorting

Thought: Insert a record into an ordered table with a sorted order to get a new ordered table with a record increasing by 1. By default, the first element is viewed as an ordered table, and then the following elements are inserted in sequence.

Time Complexity: O (n ^ 2)

Space complexity: O (1)

Stability: Stability

 

/* ** Insert sorting *: Insert a record to an ordered table in the sorted order to obtain a new ordered table with 1 Increase in the number of records, * By default, the first element is regarded as an ordered table, and the distinct element after one insertion * time complexity O (n ^ 2) * space complexity O (1) suitable for * @ param arr * @ return */public static int [] InsertSort (int [] arr) with a small number of records {// for (int I = 1; I = 0 & temp <arr [j]; j --) {// If the element to be inserted is smaller than the existing one, move it backwards, until the element is larger than the inserted element or the arr [j + 1] = arr [j];} arr [j + 1] = temp;} return arr ;}

Vi. Semi-insert sorting

 

Thought: semi-insertion sorting is rewritten based on direct insertion sorting, which can reduce the number of "moving" and "Comparing"

Time Complexity: O (n ^ 2)

Space complexity: O (1)

Stability: Stability

 

/* ** Semi-insert sorting * advantages: reduce the number of "comparisons" and "moving" * @ param arr * @ return */public static int [] BInsertSort (int [] arr) {for (int I = 1; I = high + 1; j --) {arr [j + 1] = arr [j];} arr [j + 1] = temp;} return arr ;}

VII. Hill sorting:

 

Thought: Hill sorting is also a kind of insert sorting, which is directly improved for insert sorting. This method is also called "downgrading incremental sorting ".

Time Complexity: O (n ^ 2)

Space complexity: O (1)

Stability: unstable

 

/***** Hill sorting (downgrading incremental sorting) * The Hill sorting is also a sort of insert sorting, but it has incremental, the last increment must be 1 * @ param arr * @ return */public static int [] ShellInsert (int [] arr) {int step = arr. length/2; // get the increment // ensure that the last increment is 1 while (step> = 1) {for (int I = step; I = 0 & temp 8. Merge and sort

 

Thought: merging and sorting is to combine two or more ordered tables into one ordered table. This algorithm is implemented by divide and conquer.

Time Complexity: O (nlogn)

Space complexity: O (n)

Stability: Stability

 

/* ** Merge Sorting * Merge Sorting combines two or more ordered tables into a new ordered table * time complexity O (nlog2n) * @ param arr * @ param tempArray * @ param left * @ param right * @ return */public static int [] mergeSort (int [] arr, int left, int right) {if (left <right) {// take the split position int middle = (left + right)/2; // recursively divide the left sequence mergeSort (arr, left, middle) of the array ); // recursively divide the right sequence of the array mergeSort (arr, middle + 1, right); // Merge the left and right arrays into Merge (arr, left, middle, right );} return arr;} private static void Merge (int [] arr, int left, int middle, int right) {int [] tempArray = new int [arr. length]; int leftEnd = middle; int rightStart = middle + 1; // subscript of the temporary array int tempIndex = left; int tmp = left; // when neither of the first two intervals ends, while (left <= leftEnd) & (rightStart <= right) {// The left side is smaller than the right side, insert the left-side if (arr [left] <arr [rightStart]) {tempArray [tempIndex ++] = arr [left ++];} else {tempArray [tempIndex ++] = arr [rightStart ++] ;}}// determine whether the left sequence ends while (left <= leftEnd) {tempArray [tempIndex ++] = arr [left ++];} // determines whether the right sequence ends. while (rightStart <= right) {tempArray [tempIndex ++] = arr [rightStart ++];} // 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) {arr [tmp] = tempArray [tmp ++];}

9. Base sorting

 

Thought: The base is sorted by the low position first, then collected; then sorted by the high position, then collected, and so on until the highest bit.

Note: The keywords are classified into radix boxes. When the keywords are numbers, the base number is 10. When the keywords are letters, the base number is 26.

Time Complexity: O (n + d)

Space complexity: O (n)

Stability: Stability

 

/*** Base sorting ** @ radix base indicates to classify by keyword to radix (base) box. When the keyword is a number, base 10 * @ d: The number of digits of the sorting element * @ return */public static int [] RadixSort (int [] arr, int radix, int d) {// temporary element int [] temp = new int [arr. length]; // used for counting sorting int [] count = new int [radix]; int divide = 1; for (int I = 0; I
  
   
= 0; j --) {int tempKey = (temp [j]/divide) % radix; count [tempKey] --; arr [count [tempKey] = temp [j];} divide = divide * radix;} return arr ;}
  


 

 

Public static void main (String [] args) {// basic arrangement from small to large by default // int [] disOrderArray = {3, 1, 5, 7, 0 }; // bubble sort // disOrderArray = BubbleSort (disOrderArray); // fast sort // disOrderArray = quickSort (disOrderArray, 0, disOrderArray. length-1); // select sort directly // disOrderArray = selectionSort (disOrderArray); // Heap Sort // disOrderArray = heapSort (disOrderArray ); // insert sorting directly // disOrderArray = InsertSort (disOrderArray); // semi-insert sorting (Binary Search sorting) // disOrderArray = BInsertSort (disOrderArray ); // Hill sorting // disOrderArray = ShellInsert (disOrderArray); // Merge Sorting // disOrderArray = mergeSort (disOrderArray, 0, disOrderArray. length-1); // The base sorting int [] disOrderArray = {3, 2, 3, 2, 5,333,455 78,990, 12,432, 56}; disOrderArray = RadixSort (disOrderArray, 10, 7 ); for (int I = 0; I
  
   
The basic sorting algorithms for data structures are complete.
   

 

 

Add a binary search algorithm: similar to the half-fold Search Algorithm

Time Complexity: O (logn)

 

/*** Binary Search * @ param arr * @ param searchnum the element to be searched * @ return */public static int BSearch (int [] arr, int searchnum) {int low = 0; int high = arr. length-1; while (low <= high) {int m = (low + high)/2; if (searchnum = arr [m]) {return m ;} else if (searchnum <arr [m]) {high = m-1;} else {low = m + 1 ;}} return-1 ;}


 

 

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.