Two sorting algorithms

Source: Internet
Author: User

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 (&AMP;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 (&AMP;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

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.