Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi.

Similar presentations


Presentation on theme: "COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi."— Presentation transcript:

1 COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi

2 COMP102 Lab 132 Sorting Technique to arrange a set of items in sequence Ascending Descending Sorting Applications To prepare an attendance sheet of student  Either sorted by name or ID for easy checking

3 COMP102 Lab 133 1. Selection Sort Selection sort performs sorting by repeatedly putting the largest element in the unsorted portion of the array to the end of this unsorted portion Until the whole array is sorted Algorithm 1. Define the entire array as the unsorted portion of the array 2. While the unsorted portion of the array has more than one element  Find its largest element  Swap with last element  Reduce the size of the unsorted portion of the array by 1

4 COMP102 Lab 134 1. Selection Sort – Example 71731510214Before sorting 17143151027After pass 2 17141015327After pass 3 17141075321After pass 4 17731510214After pass 1

5 COMP102 Lab 135 1. Selection Sort – Example E.g. // Sort array of integers in ascending order void select(int data[size], int size) { int temp; // for swap int max_index; // index of max value for (int rightmost=size-1; rightmost>0; rightmost--) { // find the largest item max_index = 0; for(int current=1; current<=rightmost; current++) if (data[current] > data[max_index]) max_index = current; // swap with last item if necessary if(data[max_index] > data[rightmost]) { temp = data[max_index]; data[max_index] = data[rightmost]; data[rightmost] = temp; }

6 COMP102 Lab 136 2. Bubble Sort Bubble sort examines the array from start to finish Comparing elements as it goes Any time it finds a larger element before a smaller element It swaps the two  The larger elements are passed towards the end Then it repeats the process for the unsorted portion of the array Until the whole array is sorted

7 COMP102 Lab 137 2. Bubble Sort Algorithm Define the entire array as the unsorted portion of the array While the unsorted portion of the array has more than one element 1. For every element in the unsorted portion Swap with the next neighbor if it is larger than the neighbor 2. Reduce the size of the unsorted portion of the array by 1

8 8 2. Bubble Sort – Example Before sorting14210513177 outer=7, inner=021410513177 outer=7, inner=121014513177 outer=7, inner=221051413177 outer=7, inner=321051143177 outer=7, inner=421051314177 outer=7, inner=621051314717 outer=6, inner=125101314717 outer=6, inner=225110314717 outer=6, inner=325131014717 outer=6, inner=525131071417 outer=5, inner=121531071417 outer=5, inner=221351071417 outer=5, inner=421357101417 outer=4, inner=012357101417 … outer=1, inner=012357101417

9 COMP102 Lab 139 2. Bubble Sort – Example E.g. // Sort an array of integers in ascending order void bubble(int data[], int size) { int temp; for (int outer=size-1; outer>0; outer--) { for (int inner=0; inner < outer; inner++) { // traverse the nested loops // if the current element is greater // swap current element with next if ( data[inner] > data[inner+1] ) { temp = data[inner]; data[inner] = data[inner+1]; data[inner+1] = temp; } } // inner for loop } // outer for loop }

10 COMP102 Lab 1310 SUMMARY By the end of this lab, you should be able to: Sorting an unordered array with  Selection sort  Bubble sort


Download ppt "COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi."

Similar presentations


Ads by Google