1 / 5

Sorting

Sorting. Sorting. A common activity for programmers to do is sort. You’ve been asked to do this for your last project. There are many ways to sort data We will look at sorting arrays You will have to generalize this to sorting an ArrayList. Two Basic Sorts. Bubble sort A horrible sort.

koto
Télécharger la présentation

Sorting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Sorting

  2. Sorting • A common activity for programmers to do is sort. • You’ve been asked to do this for your last project. • There are many ways to sort data • We will look at sorting arrays • You will have to generalize this to sorting an ArrayList

  3. Two Basic Sorts • Bubble sort • A horrible sort. • Very slow and no redeeming qualities • But it is very easy to implement • Selection sort • Not much better than bubble sort • It is still slow but not as slow as bubble sort • It is also very easy to implement

  4. Selection Sort for (passCount = 0; passCount<length-1;passCount++ ) { minIndx = passCount; for (srchIndx=passCount+1;srchIndx<length;srchIndx++) if ( data[srchIndx] < data[minIndx] ) minIndx = srchIndx; temp = data[minIndx]; data[minIndx] = data[passCount]; data[passCount] = temp; } }

  5. Things to Note • The temp variable used in the swapping, must be of the same type as the array. • minIndex, passCount, srchIndex, length are all integers • Length is the length of the array • More specifically, it is the number of locations in the array that have been used.

More Related