Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.

Similar presentations


Presentation on theme: "Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012."— Presentation transcript:

1 Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012

2 Overview  Why we do sorting?  What is Sorting?  Types of Sorting  Bubble Sort  Selection Sort  Insertion Sort

3 Why we do sorting?  Commonly encountered programming task in computing.  Examples of sorting: 1.List containing exam scores sorted from Lowest to Highest or from Highest to Lowest 2.List containing words that were misspelled and be listed in alphabetical order. 3.List of student records and sorted by student number or alphabetically by first or last name.

4 Why we do sorting?  Searching for an element in an array will be more efficient. (example: looking up for information like phone number).  It’s always nice to see data in a sorted display. (example: spreadsheet or database application).  Computers sort things much faster.

5 What is Sorting?  Sorting is an operation that puts elements of a list in certain order such as numerical or alphabetical order.  Example: 41012135057631002 41012135057631002

6 Types of Sorting  There are many, many different types of sorting algorithms, but the primary ones are: ● Bubble Sort ● Selection Sort ● Insertion Sort ● Merge Sort ● Shell Sort ● Heap Sort ● Quick Sort ● Radix Sort ● Swap Sort

7  Most of the primary sorting algorithms run on different space and time complexity.  Time Complexity is defined to be the time the computer takes to run a program (or algorithm in our case).  Space complexity is defined to be the amount of memory the computer needs to run a program. Review of Complexity

8 Bubble Sort  Compare each element (except the last one) with its neighbor to the right  If they are out of order, swap them  This puts the largest element at the very end  The last element is now in the correct and final place  Compare each element (except the last two) with its neighbor to the right If they are out of order, swap them This puts the second largest element next to last The last two elements are now in their correct and final places  Compare each element (except the last three) with its neighbor to the right  Continue as above until you have no unsorted elements on the left

9 Example of Bubble Sort 7 2854 2 7854 2 7854 2 7584 2 7548 2 7548 2 5748 2 5478 2 7548 2 5478 2 4578 2 5478 2 4578 2 4578 (done)

10 Code for Bubble Sort public static void bubbleSort(int[] a) { int outer, inner; for (outer = a.length - 1; outer > 0; outer--) { // counting down for (inner = 0; inner a[inner + 1]) { // if out of order... int temp = a[inner]; //...then swap a[inner] = a[inner + 1]; a[inner + 1] = temp; } } } }

11 Selection Sort  Given an array of length n,  Search elements 0 through n-1 and select the smallest  Swap it with the element in location 0  Search elements 1 through n-1 and select the smallest  Swap it with the element in location 1  Search elements 2 through n-1 and select the smallest  Swap it with the element in location 2  Search elements 3 through n-1 and select the smallest  Swap it with the element in location 3  Continue in this fashion until there’s nothing left to search

12 12 7 2854 2 7854 2 4857 2 4587 2 4578 Example of Selection Sort

13 Code for Selection Sort public static void selectionSort(int[] a) { int outer, inner, min; for (outer = 0; outer < a.length - 1; outer++) { // outer counts down min = outer; for (inner = outer + 1; inner < a.length; inner++) { if (a[inner] < a[min]) { min = inner; } } int temp = a[outer]; a[outer] = a[min]; a[min] = temp; } }

14 Insertion Sort  Insertion sort keeps making the left side of the array sorted until the whole array is sorted. It sorts the values seen far away and repeatedly inserts unseen values in the array into the left sorted array.  Real life example: An example of an insertion sort occurs in everyday life while playing cards. To sort the cards in your hand you extract a card, shift the remaining cards, and then insert the extracted card in the correct place. This process is repeated until all the cards are in the correct sequence.

15  Sort: 34 8 64 51 32 21  34 8 64 51 32 21 The algorithm sees that 8 is smaller than 34 so it swaps.  8 34 64 51 32 21 51 is smaller than 64, so they swap.  8 34 51 64 32 21 Example of Insertion Sort

16  Sort: 34 8 64 51 32 21  8 34 51 64 32 21 (from previous slide) The algorithm sees 32 as another smaller number and moves it to its appropriate location between 8 and 34.  8 32 34 51 64 21 The algorithm sees 21 as another smaller number and moves into between 8 and 32.  Final sorted numbers:  8 21 32 34 51 64 Example of Insertion Sort

17 Code for Insertion Sort void insertion_sort(int x[],int length) { int key,i; for(int j=1;j<length;j++) { key=x[j]; i=j-1; while(x[i]>key && i>=0) { x[i+1]=x[i]; i--; } x[i+1]=key; }


Download ppt "Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012."

Similar presentations


Ads by Google