Algorithm: three simple sorting algorithms

Source: Internet
Author: User

sorting algorithms are more common: bubble sort, simple selection sort, direct insert sort, hill sort, heap sort, merge sort and fast sort algorithm, etc. Learn the first three simple algorithms today. Related concepts of sorting:

① the stability of the sort: two or more elements are equal, and the order is still the same, and the ordering is stable.

② internal ordering: The sorting process is done in memory; external sort: a sort procedure that requires external access.

③ ranking Algorithm performance factors: 1, time performance, comparison and movement, 2, auxiliary space; 3, algorithmic complexity


Example: Bubble sort, simple select sort and direct Insert sort

#include "stdio.h" #define MAXSIZE 6int data[maxsize] = {0,5,4,3,2,1};/** function: array element Exchange * Input: data, swap element subscript * Output: no */void swap (int Data[],int I,int j) {int temp = Data[i];d ata[i] = data[j];d ata[j] = temp;} /** function: Bubble sort * Input: Array * output: no * algorithm: Loop once, subscript location is the minimum value, constantly compare and move */void bubble1 (int data[]) {for (int i = 0; i < MAXSIZE-1; i++) for (i NT J = i+1; J < MAXSIZE; J + +) if (Data[j] < data[i]) swap (DATA,I,J);}  /** function: Improved bubble sort * Input: Array * output: no * algorithm: Improved algorithm in addition to focus on the current minimum value, you can also move other smaller values in front */void bubble2 (int data[]) {int flag = 1;  Flag bit for (int i = 0; i < MAXSIZE-1 && flag; i++) {flag = 0; Each loop flag bit is assigned 0for (int j = MAXSIZE-1; j > i; j--) if (Data[j] < data[j-1])//adjacent to two elements before the comparison, rather than the above algorithm focusing only on the minimum value.  {swap (DATA,J-1,J); flag = 1; When a move occurs, it is assigned to 1, otherwise the sort is completed without unnecessary comparisons. }}}/** function: Simple selection sort * Input: Array * output: no * algorithm: Compare the lowest value of the subscript, the last move, instead of each comparison after the move */void easy (int data[]) {int min = 0;for (int i = 0; i < MAX SIZE-1;  i++) {min = i; Suppose that the first subscript is the minimum value for (int j = i+1; J < MAXSIZE; J + +) {if (Data[j] < data[min])//When there is less data than the first subscript, record its subscript min = j;}  if (min! = i)Swap swap If the minimum value is not the first subscript (data,min,i);}} /** function: Direct Insert sort * Input: Array * output: no * algorithm: Insert data into the ordered table that has already been ordered, get the ordered table with record increment 1.  */void insertsort (int data[]) {for (int i = 2; i < MAXSIZE; i++)//First loop, sort data[1],data[2] two elements, data[0] is Sentinel action {if (Data[i] < data[i-1])//If DATA[2] is smaller than data[1], Sentinel Data[0] set to data[2]{data[0] = data[i];int j = i;while (Data[j-1] > Data[0])//When DATA[1] > Data[0], move it right to data[2] = data[1]{data[j] = data[j-1];j--;}  DATA[J] = data[0]; DATA[1] = data[0], to achieve the exchange of data[1] and data[2],//At this time data[1] and data[2] is ordered orderly table, the beginning of the second loop}}}void print (int *data) {for (int i = 0; i < MAXSIZE; i++) printf ("%d", Data[i]);p rintf ("\ n");} int main (int argc, char* argv[]) {print (data),//bubble1 (data),//bubble2 (data),//simple (data), Insertsort (data);p rint (data);}

Summary: The time complexity of the three simple sorting algorithms is O (n^2), which is a stable sort.



Algorithm: three simple 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.