For several days did not write a blog, recently to test, have C language. Originally prepared to deal with the exam, casually learn, and then prepare for the next semester with C to learn the data structure and algorithm, but, watched some video, ready to enter the pit C/s .... I learned nearly a semester of C #, so give up feeling or some small reluctant to ... But technology is common, and it's not a waste of time.
Then post the code:
1 intnum[Ten];2 time_t TMS;3Srand ((unsignedint) time (&TMS));//seed Generation4 for(inti =0; I <Ten; i++)//to generate an array of array element 105 {6Num[i] = -+ rand ()% -;7printf"%d\n", Num[i]);8 }9 Ten One //Bubble Sort A for(inti =0; I <Ten-1; i++) - { - the for(intj =0; J <Ten-1I J + +) - { - //int max=0;//num is subscript - if(num[j]>num[j+1]) + { - intTMP =Num[j]; +NUM[J] = num[j+1]; Anum[j+1] =tmp; at } - } -}
1 intnum[Ten];2 time_t TMS;3Srand ((unsignedint) time (&TMS));//seed Generation4 for(inti =0; I <Ten; i++)//to generate an array of array element 105 {6Num[i] = -+ rand ()% -;7printf"%d\n", Num[i]);8 }9 Ten One //Select Sort A for(inti =0; I <Ten; i++) - { - intmax=0;//The maximum number of subscripts, starting with the default number of first the for(intj =0; J <TenI J + +) - { - - if(Num[max] <Num[j]) + { -Max =J; + } A } at - intTMP = num[Ten-1-i]; -num[Ten-1-I] =Num[max]; -Num[max] =tmp; - -}
Above is the implementation code for bubble sort and select sort
Bubble sort, that is, every time you start bubbling from the beginning, choose the big one, continue to the back, a round down, the biggest on the last side, the next round of bubbles can find the second-largest number in the penultimate position .... Repeated....
Choose Sort, that is, each time you find the maximum number of subscript, and then put the largest number to the last.
I personally think that the two sorting methods are essentially the same, are compared after the exchange, bubbling exchange is the value itself, and the choice of exchange is the array subscript.
In the case of a pure int array of numbers, I think the efficiency is basically the same, because the index of the array and the sizeof of the elements are the same, which is 4 bytes.
Of course, these are my blind thinking, exactly how, I believe after I learned the data structure and algorithm, can get the correct answer.
Two sorting algorithms