1 / 15

Sorting Algorithms for Dynamic Records

Learn about advanced algorithms for sorting dynamic records in computer programming, including select sort, insert sort, bubble sort, and modified bubble sort. Example codes in C are provided.

rigney
Download Presentation

Sorting Algorithms for Dynamic Records

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. Computers and programming The9th lecture Jiří Šebesta

  2. TOPIC – advanced algorithms Sorting algorithms Sorting for dynamic records

  3. SELECTSORT – principle for upward sorting Sorting algorithms (1/10) The smallest element from the field is chosen and replaced by the first one (with index 0). Then the smallest element from the field from index 1 is searched and replaced by the first element of whole field (with index 1). The same procedure is done for following elements. This algorithm is based on minimum searching ( or maximum for downward sorting) sequentially for the field truncated by one element from left in each round.

  4. SELECTSORT – algorithm in C Sorting algorithms (2/10) for(i =0;i <(N-1); i++) { k=i; minim=vect[i]; for(j=i+1; j<N; j++) if(vect[j]<minim) { k = j; minim = vect[j]; } vect[k]=vect[i]; vect[i]=minim; } Number of elements is N Sorted vector is vect[]

  5. Example of sequential sorting by SELECTSORT for 20 integers Sorting algorithms (3/10) Example: Ex71.c

  6. INSERTSORT– principle for upward sorting Sorting algorithms (4/10) All elements excluding the first one are sequentially picked up (i.e. from the second element of the sorted field) and inserted to the position of the field according to carrying value, other elements are shifted (the same as card player sorts cards during giving out).

  7. INSERTSORT – algorithm in C Sorting algorithms (5/10) for(i=2; i<=N; i++) { vect[0]= vect[i]; j = i-1; while(vect[0]<vect[j]) { vect[j+1] = vect[j]; j--; } vect[j+1]= vect[0]; } Number of elements is N Sorted vector is vect[] from position 1 to N,element vect[0] is determined for actual sorting value

  8. Example of sequential sorting by INSERTSORT for 20 integers Sorting algorithms (6/10) Example: Ex72.c

  9. BUBLESORT– principle for upward sorting Sorting algorithms (7/10) Sequentially, two neighbouring elements are tested, if the second is smaller, both elements are mutually exchanged. Next tested elements are this second one and following one. This procedure must be done N-1 times over all vector.

  10. BUBLESORT – algorithm in C Sorting algorithms (8/10) for(i=0; i<(N-1); i++) for(j=0; j<(N-1); j++) { if(vect[j]>vect[j+1]) { aux = vect[j]; vect[j] = vect[j+1]; vect[j+1] = aux; } } Number of elements is N Sorted vector is vect[]

  11. Example of sequential sorting by BUBLESORT for 20 integers Sorting algorithms (9/10) Example: Ex73.c

  12. MODIFIED BUBLESORT Sorting algorithms (10/10) Occurrence of element exchange is tested during sorting of the vector. If element exchange does not occurred in the last round, sorting is finished. for(i=0; i<(N-1); i++) { test = 0; for(j=0; j<(N-1); j++) {if(vect[j]>vect[j+1]) { aux = vect[j]; vect[j] = vect[j+1]; vect[j+1] = aux; test = 1; } } if(!test) break; } Example: Ex74.c

  13. MODIFIED BUBLESORTFOR LINEAR LIST SORTING Sorting for dynamic records (1/2) Example: Build-up an algorithm which sorts a linear unsorted list of dynamically created records. The record structure represents a broad jumper with items: competitor name name and length of jump jump. Apply modified bublesort for sorting according to length of jump.

  14. Sorting algorithm for records from linear unsorted list: Sorting for dynamic records (2/2) Programming in class Example: Ex75.c

  15. TOPIC OF THE NEXT LECTURE Windows form application in Microsoft Visual Studio THANK YOUFOR YOUR ATTENTION

More Related