1 / 20

Sorting

Sorting. Leena A PGT Comp SC KV No:2 Jalahalli. Introduction. Common problem: sort a list of values, starting from lowest to highest. List of exam scores Words of dictionary in alphabetical order Students names listed alphabetically Student records sorted by ID#.

Download Presentation

Sorting

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. Sorting Leena A PGT Comp SC KV No:2 Jalahalli

  2. Introduction • Common problem: sort a list of values, starting from lowest to highest. • List of exam scores • Words of dictionary in alphabetical order • Students names listed alphabetically • Student records sorted by ID#

  3. Quadratic Sorting Algorithms • We are given n records to sort. Three Sorting techniques are used normally • Selection sort • Insertion sort • Bubble sort

  4. Example: we are given an array of six integers that we want to sort from smallest to largest Sorting an Array of Integers [0][1] [2] [3] [4] [5]

  5. Start by finding the smallest entry. The Selection Sort Algorithm [0][1] [2] [3] [4] [5]

  6. Swap the smallest entry with the first entry. The Selection Sort Algorithm [0][1] [2] [3] [4] [5]

  7. Swap the smallest entry with the first entry. The Selection Sort Algorithm [0][1] [2] [3] [4] [5]

  8. Part of the array is now sorted. The Selection Sort Algorithm Sorted side Unsorted side [0][1] [2] [3] [4] [5]

  9. Find the smallest element in the unsorted side. The Selection Sort Algorithm Sorted side Unsorted side [0][1] [2] [3] [4] [5]

  10. Swap with the front of the unsorted side. The Selection Sort Algorithm Sorted side Unsorted side [0][1] [2] [3] [4] [5]

  11. We have increased the size of the sorted side by one element. The Selection Sort Algorithm Sorted side Unsorted side [0][1] [2] [3] [4] [5]

  12. The process continues... The Selection Sort Algorithm Sorted side Unsorted side Smallest from unsorted [0][1] [2] [3] [4] [5]

  13. The process continues... The Selection Sort Algorithm Sorted side Unsorted side Swap with front [0][1] [2] [3] [4] [5]

  14. The process continues... The Selection Sort Algorithm Sorted side is bigger Sorted side Unsorted side [0][1] [2] [3] [4] [5]

  15. The process keeps adding one more number to the sorted side. The sorted side has the smallest numbers, arranged from small to large. The Selection Sort Algorithm Sorted side Unsorted side [0][1] [2] [3] [4] [5]

  16. We can stop when the unsorted side has just one number, since that number must be the largest number. Sorted side Unsorted side The Selection Sort Algorithm [0][1] [2] [3] [4] [5]

  17. The array is now sorted. We repeatedly selected the smallest element, and moved this element to the front of the unsorted side. The Selection Sort Algorithm [0][1] [2] [3] [4] [5]

  18. void selectionsort(int a[], int size) { int small,pos,t; for(int i=0;i<size;i++) { small=a[i]; for(int j= i+1;j<size;j++) { if(a[j]<small) { small=a[j]; pos=j; } } t=a[i]; a[i]=a[pos]; a[pos]=t; }}

  19. Review Questions • Write a function to sort the student records based on their marks in ascending order. • The records are implemented as follows • struct student • { • intrno; • char name[20]; • float marks; • };

  20. Thank You

More Related