Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 14: A Programming Example: Sorting Yoni Fridman 7/24/01 7/24/01.

Similar presentations


Presentation on theme: "Lecture 14: A Programming Example: Sorting Yoni Fridman 7/24/01 7/24/01."— Presentation transcript:

1 Lecture 14: A Programming Example: Sorting Yoni Fridman 7/24/01 7/24/01

2 OutlineOutline ä Overview ä Insertion sort ä Inserting into a sorted array ä The algorithm ä The code ä Selection sort ä The algorithm ä The code ä Overview ä Insertion sort ä Inserting into a sorted array ä The algorithm ä The code ä Selection sort ä The algorithm ä The code

3 OverviewOverview ä Beyond learning to program, computer science is based on the ability to design algorithms that solve given problems. ä A classic computer science problem is sorting: How do you take a scrambled list and put its elements in order as efficiently as possible? ä The problem of sorting a list of elements has been studied extensively over the last 40 years, and many sorting algorithms exist – we’ll look at insertion sort and selection sort. ä Beyond learning to program, computer science is based on the ability to design algorithms that solve given problems. ä A classic computer science problem is sorting: How do you take a scrambled list and put its elements in order as efficiently as possible? ä The problem of sorting a list of elements has been studied extensively over the last 40 years, and many sorting algorithms exist – we’ll look at insertion sort and selection sort.

4 Inserting Into a Sorted Array  Suppose we already have a sorted array of int ’s, and we want to insert a new element into it. ä We would compare the new element to each element of the sorted array, one at a time, until we find it’s spot.  Suppose we already have a sorted array of int ’s, and we want to insert a new element into it. ä We would compare the new element to each element of the sorted array, one at a time, until we find it’s spot. 2 2 4 4 5 5 8 8 8 8 11 12 15 16 20 2 2 4 4 5 5 8 8 8 8 12 15 16 20 11

5 Inserting Into a Sorted Array ä Now, suppose we start with an empty array, and keep inserting new elements into it, one at a time. 5 5 8 8 12 5 5 8 8 5 5

6 Insertion Sort ä Now, suppose we start with an unsorted array of n elements. We can think of element 0 as being sorted, and elements 1 through n-1 as being unsorted. ä We look at the first unsorted element and insert it into the sorted portion of the array. Now, we have elements 0 and 1 sorted and elements 2 through n-1 unsorted. ä Then we look at the first element of the remaining unsorted portion and insert it into the sorted portion of the array. ä Etc. ä Now, suppose we start with an unsorted array of n elements. We can think of element 0 as being sorted, and elements 1 through n-1 as being unsorted. ä We look at the first unsorted element and insert it into the sorted portion of the array. Now, we have elements 0 and 1 sorted and elements 2 through n-1 unsorted. ä Then we look at the first element of the remaining unsorted portion and insert it into the sorted portion of the array. ä Etc. 5 5 8 8 12 5 5 8 8 5 5 8 8

7 Insertion Sort ä Let’s look at the pseudocode for insertion sort: For each unsorted element i, where i = 1 to n-1. Look at each element j, where j = i - 1 down to 0, until element j <= element i. Insert element i immediately after element j. ä And let’s translate it into source code: for (int i = 1; i < N; i++) { j = i - 1; while (j >= 0 && array[i] = 0 && array[i] < array[j]) array[j+1] = array[j--]; array[j+1] = array[i]; ä Let’s look at the pseudocode for insertion sort: For each unsorted element i, where i = 1 to n-1. Look at each element j, where j = i - 1 down to 0, until element j <= element i. Insert element i immediately after element j. ä And let’s translate it into source code: for (int i = 1; i < N; i++) { j = i - 1; while (j >= 0 && array[i] = 0 && array[i] < array[j]) array[j+1] = array[j--]; array[j+1] = array[i];

8 Selection Sort ä The selection sort algorithm works by selecting the smallest unsorted element each iteration, and putting it ahead of all the other unsorted elements. ä Looking at the whole array, we find that the smallest element is 5, so we place it in the first slot by swapping it with the 12. Now the 5 is sorted, but the 12 and 8 aren’t. ä So we look at the 12 and 8. The 8 is the smallest element left, so we place it in the first unsorted slot by swapping it with the 12. ä Once we’ve sorted elements 0 through n-2, the last element is guaranteed to be sorted. ä The selection sort algorithm works by selecting the smallest unsorted element each iteration, and putting it ahead of all the other unsorted elements. ä Looking at the whole array, we find that the smallest element is 5, so we place it in the first slot by swapping it with the 12. Now the 5 is sorted, but the 12 and 8 aren’t. ä So we look at the 12 and 8. The 8 is the smallest element left, so we place it in the first unsorted slot by swapping it with the 12. ä Once we’ve sorted elements 0 through n-2, the last element is guaranteed to be sorted. 5 5 8 8 12 5 5 8 8 5 5 8 8

9 Selection Sort ä Let’s look at the pseudocode for selection sort: For each element i, where i = 0 to n-2. Look at each unsorted element j (j = i to n-1) in order to find the smallest unsorted element. Swap the smallest element with element i. ä And let’s translate it into (kind of) source code: for (int i = 0; i < N-1; i++) { for (int j = i; j < N; j++) if (array[j] is the smallest element so far) Remember that j is the smallest Swap the smallest element with element i. ä Let’s look at the pseudocode for selection sort: For each element i, where i = 0 to n-2. Look at each unsorted element j (j = i to n-1) in order to find the smallest unsorted element. Swap the smallest element with element i. ä And let’s translate it into (kind of) source code: for (int i = 0; i < N-1; i++) { for (int j = i; j < N; j++) if (array[j] is the smallest element so far) Remember that j is the smallest Swap the smallest element with element i.

10 HomeworkHomework ä Read: 6.1 and 6.2.


Download ppt "Lecture 14: A Programming Example: Sorting Yoni Fridman 7/24/01 7/24/01."

Similar presentations


Ads by Google