Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computers and programming The 9 th lecture Jiří Šebesta.

Similar presentations


Presentation on theme: "Computers and programming The 9 th lecture Jiří Šebesta."— Presentation transcript:

1 Computers and programming The 9 th lecture Jiří Šebesta

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

3 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. SELECTSORT – principle for upward sorting 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 Sorting algorithms (2/10) SELECTSORT – algorithm in C 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 Sorting algorithms (3/10) Example: Ex71.c Example of sequential sorting by SELECTSORT for 20 integers

6 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). INSERTSORT – principle for upward sorting

7 Sorting algorithms (5/10) INSERTSORT – algorithm in C 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 Sorting algorithms (6/10) Example: Ex72.c Example of sequential sorting by INSERTSORT for 20 integers

9 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. BUBLESORT – principle for upward sorting

10 Sorting algorithms (8/10) BUBLESORT – algorithm in C 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 Sorting algorithms (9/10) Example: Ex73.c Example of sequential sorting by BUBLESORT for 20 integers

12 Sorting algorithms (10/10) MODIFIED BUBLESORT 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; } Occurrence of element exchange is tested during sorting of the vector. If element exchange does not occurred in the last round, sorting is finished. Example: Ex74.c

13 Sorting for dynamic records (1/2) MODIFIED BUBLESORT FOR LINEAR LIST SORTING 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 for dynamic records (2/2) Sorting algorithm for records from linear unsorted list: Example: Ex75.c Programming in class

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


Download ppt "Computers and programming The 9 th lecture Jiří Šebesta."

Similar presentations


Ads by Google