[MOOC Notes] sorting topics (data structure)

Source: Internet
Author: User

1. Bubble Sort:

Idea: to exchange the adjacent reverse elements in order, until the whole sequence, the algorithm is as follows:

/** * Bubble Sort-Initially implemented, Time complexity O (n^2) * @param arr array to sort * @param the start position of the lo to sort interval * @param the end position of the range to be sorted */public static void BubbleS ORT (int[] arr, int lo, int hi) {//scale n-1 scan for sequence for (int i = lo; i < hi-1; i++) {//each scan will move the largest element of the current interval to the end, so the range of each scan can be reduced 1for (int j = lo; j < Hi-i 1; j + +) {//Determine if adjacent elements are in reverse order, if (Arr[j] > arr[j + 1]) {swap (arr, J, J + 1)}}}

The above algorithm n-1 a sequence of n in any case, even if the sequence has become orderly in the middle. So a marker bit is required to record whether the sequence has been sorted after each scan, and the improved algorithm is as follows:

/** * Bubble sort-improved one: Record scan results, time complexity O (n^ (3/2)) * @param arr array to sort * @param the starting position of the lo to sort interval * @param the end position of the range to be sorted */public static VO  ID bubblesort (int[] arr, int lo, int hi) {//scale n-1 scan for sequence for (int i = lo; i < hi-1; i++) {//Set an indicator bit record sequence is an ordered state Boolean sorted = true;//Each scan will move the largest element of the current interval to the end, so the range of each scan can be reduced by 1for (int j = lo; j < hi-i-1, j + +) {//To determine if the adjacent element is in reverse order, to exchange and to place an ordered Falseif (Arr[j] > arr[j + 1]) {swap (arr, J, j + 1); sorted = false;}} When an ordered flag is thrown to true after a scan, the sequence is ordered, stop scanning if (sorted) {break;}}}


The improved algorithm is still defective, even though the number of scans has been reduced dynamically, but the range of each scan is fixed. And then the improvement:

/** * Bubble sort-improved two: Record reverse range, time complexity O (n) * @param arr array to sort * @param the starting position of the lo to sort interval * @param hi to the end of the sort interval */public static void Bub  Blesort (int[] arr, int lo, int hi) {//scan the while (Lo < hi) {//Set the rank to the starting position of the interval before the interval that may be reversed) {//sets the order to the beginning of the range int = lo;//scans the entire interval for (int j = Lo J < Hi-1; J + +) {//To determine if the adjacent element is in reverse order, then swap and record the position if (Arr[j] > arr[j + 1]) {swap (arr, J, j + 1); last = j + 1;}} Sets the position of the last interchange to the end of the interval (all subsequent elements are ordered) Hi = Final;}}


Three time costs for bubble sorting:



2. Select Sort:

Idea: the maximum (small) element of each selection interval is moved to the interval front end, until all elements are selected, the algorithm is as follows:

/** * Select Sort, time complexity O (n^2) * @param arr array to sort * @param the starting position of the lo to sort interval * @param hi to the end position of the sorted interval */public static void Selectionsor T (int[] arr, int lo, int hi) {//Size n-1 scan for (int i = lo; i < hi-1; i++) {int max = Arr[i], index = i;//Each scan is recorded when The smallest element of the front interval for (int j = i + 1; j < Hi; j + +) {if (Arr[j] < max) {max = Arr[j];index = j;}} Move the smallest element to the front of the interval (index! = i) {swap (arr, I, index);}}}

The primary time cost for selecting a sort is the largest element in the selection interval, the time complexity of the operation is O (n), and there is a way to reduce the time cost to O (Logn), which will be described in the subsequent heap ordering.


3. Merge sort

Idea: Divide the whole sequence into multiple sub-sequences, sequence the subsequence sequentially, and finally merge into an ordered sequence, with the following algorithm:

/** * Merge sort, time complexity O (NLOGN) * @param arr array to sort * @param the starting position of the lo to sort interval * @param hi to the end of the sort interval */public static void MergeSort ( Int[] arr, int lo, int hi) {//If there is only one element left in the interval, the recursive if (Hi-lo < 2) {return;} Split two interval int mid = (hi + lo) >> 1;//two interval mergesort (arr, lo, mid) respectively, MergeSort (arr, Mid, HI),//merge two zones (arr , Lo, Mid, hi);} /** * Merge interval for merged sort * @param arr array to be sorted * @param lo to merge the starting position of the interval * @param mid-interval separated points * @param hi to the end position of the merge interval */private static voi  D merge (int[] arr, int lo, int mid, int hi) {//create temporary array backup to sort interval int[] temp = new Int[hi-lo];for (int i = lo; i < hi; i++) {Temp[i-lo] = arr[i];} Set the starting position of the two rank to point to two temporary intervals, set a rank to point to the beginning of the entire interval int i = 0, j = mid-lo, k = lo;//Stop while the rank of the entire interval points to the end position of the interval (K < hi) {//The element that points to two rank The smaller ones are stored in the to-sort interval if ((I < Mid-lo) && (j >= Hi-lo | | temp[i] < TEMP[J])) {arr[k++] = temp[i++];} if ((J < Hi-lo) && (i >= Mid-lo | | temp[j] < temp[i])) {arr[k++] = temp[j++];}}


4. Insert Sort

Idea: Take each element out and insert it in the appropriate position so that the element is in order with its previous and subsequent elements until all the elements have been inserted. The algorithm is as follows:

/** * Insert Sort, time complexity O (n^2) * @param arr array to sort * @param the starting position of the lo to sort interval * @param hi to the end position of the sorted interval */public static void Insertionsor T (int[]arr, int lo, int hi) {//scan each element for (int i = lo + 1; i < hi; i++) {//If the element is smaller than its previous element if (Arr[i] < arr[i-1]) {/ /record its position and value int temp = Arr[i];int index = i;//The element larger than it moves back while (Index > 0 && arr[index-1] > Temp) {arr[index ] = arr[index-1];index--;} Insert it into the appropriate position arr[index] = temp;}}}

Not to be continued ....

[MOOC Notes] sorting topics (data structure)

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.