Bubble sort (Bubble sort) basic idea: 22 Compare the keywords of adjacent records, if the reverse order is exchanged until there are no reversed records. The time complexity is O (N2).
The basic idea of simple Selection sort is to select the smallest record of a keyword from a n-i+1 record and exchange it with the first record by comparing N-i keywords. It should be said that although the same as the bubble sort is O (N2), the simple selection of the sort performance is slightly better than the bubble sort.
The basic idea of inserting a sort directly (straight insertion sort) is to insert a record into an ordered table that has been sorted before, thus obtaining a new, sequential table with a number of 1 records. The time complexity of the direct insertion sorting method is O (N2). It is also seen from here that the same
O (N2) time complexity, direct insertion sort Fabienne The performance of the cannon and simple selection sorting is better.
1 //bubble Sort, simple select sort, direct insert sort2#include"stdafx.h"3 4#include"stdio.h" 5 #defineTRUE 16 #defineFALSE 07 8 voidBubblesort (int*list,intindex);9 voidSelectsort (int*list,intindex);Ten voidInsertsort (int*list,intindex); One Atypedefintstatus; - intlist[ -];//The default maximum length is - inttemp; the intI, J; - intnode; -Status change =TRUE; - + voidSwapint*list,intIintJ//Interchange Element - { +temp =List[i]; AList[i] =List[j]; atLIST[J] =temp; - } - - voidMainvoid) - { - intindex =0; inprintf"Please input the value of want to sort (exit for 0): \ n"); -scanf_s ("%d", &node); to while(Node! =0) + { -List[index] =node; theIndex + =1; *scanf_s ("%d", &node); $ }Panax Notoginseng bubblesort (list, index); - selectsort (list, index); the insertsort (list, index); + } A the voidBubblesort (int*list,intIndex//Bubble sort Time complexity O (n2) + { - for(j =0; J<index && Change; J + +) $ { $Change =FALSE; - for(i =1; i<index-j; i++) - { the if(List[i-1]>List[i]) - {WuyiSwap (list, I-1, i); theChange =TRUE; - } Wu } - } Aboutprintf"\ nthe bubble sort is:"); $ for(i =0; I < index; i++) - { -printf"%d", List[i]); - } A + } the - voidSelectsort (int*list,intIndex//simple selection of sorting time complexity O (n2) $ { the intmin; the for(i =0; I < index-1; i++) the { theMin =i; - for(j = i +1; J < Index; J + +) in { the if(list[i]>List[j]) the { AboutMin =J; the } the if(min! =i) the { + Swap (list, Min, i); - } the }Bayi } theprintf"\ nthe simple selection after sorting is:"); the for(i =0; I < index; i++) - { -printf"%d", List[i]); the } the } the the voidInsertsort (int*list,intIndex//Direct insertion sequencing time complexity O (n2) - { the for(i =1; I < index; i++) the { the if(List[i-1]>List[i])94 { thetemp =List[i]; the for(j = i; J >=1&& list[j-1] > temp; j--) the {98LIST[J] = list[j-1]; About } -LIST[J] =temp;101 }102 }103printf"\ n The direct insert sort is:");104 for(i =0; I < index; i++) the {106printf"%d", List[i]);107 }108}
Bubble sort, simple select sort, direct insert sort