Docsity

Lecture Notes on Searching and Sorting Algorithms - Prof. Debopam Acharya, Study notes of Data Structures and Algorithms

Partial preview of the text

Download Lecture Notes on Searching and Sorting Algorithms - Prof. Debopam Acharya and more Study notes Data Structures and Algorithms in PDF only on Docsity!

M5 Lecture Notes – Searching and Sort ing 1

Searching and Sorting

M5 Lecture Notes – Searching and

Sorting Algorithms and Their Efficiency

Sorting

 (^) A process that organizes a collection of data into either ascending or descending order 

Categories of sorting algorithms

 (^) An internal sort  (^) Requires that the collection of data fit entirely in the computer’s main memory  (^) An external sort  (^) The collection of data will not fit in the computer’s main memory all at once but must reside in secondary storage

M5 Lecture Notes – Searching and

Sorting and Searching Algorithms

 (^) In this lecture, we are discussing 5 sorting and 1 searching algorithms.  (^) Sorting Algorithms  (^) Selection sort  (^) Bubble sort  (^) Merge Sort  (^) Insertion sort  (^) Quick sort  (^) Searching Algorithm  (^) Binary Search

M5 Lecture Notes – Searching and

Selection Sort

 (^) Strategy  (^) Select the largest item and put it in its correct place  (^) Select the next largest item and put it in its correct place, etc. A selection sort of an array of five integers

M5 Lecture Notes – Searching and

Bubble Sort

 (^) Strategy  (^) Compare adjacent elements and exchange them if they are out of order  (^) Comparing the first two elements, the second and third elements, and so on, will move the largest (or smallest) elements to the end of the array  (^) Repeating this process will eventually sort the array into ascending (or descending) order

M5 Lecture Notes – Searching and

Bubble Sort

The first two passes of a bubble sort of an array of five integers: a) pass 1; b) pass 2

M5 Lecture Notes – Searching and

Mergesort

 (^) Important divide-and-conquer sorting algorithms  (^) Mergesort  (^) Quicksort  (^) Mergesort  (^) A recursive sorting algorithm  (^) Gives the same performance, regardless of the initial order of the array items  (^) Strategy  (^) Divide an array into halves  (^) Sort each half  (^) Merge the sorted halves into one sorted array

M5 Lecture Notes – Searching and

Mergesort

A mergesort with an auxiliary temporary array

M5 Lecture Notes – Searching and

Mergesort

Analysis

 (^) Worst case: O(n * log 2 n)  (^) Average case: O(n * log 2 n)  (^) Advantage  It is an extremely efficient algorithm with respect to time  (^) Drawback  It requires a second array as large as the original array

M5 Lecture Notes – Searching and

Insertion sort

 (^) Let a 0 , ...,^ an -1 be the sequence to be sorted. At the beginning and after each iteration of the algorithm the sequence consists of two parts: the first part a 0 , ..., ai -1 is already sorted, the second part ai , ..., an -1 is still unsorted.  (^) In order to insert element a i into the sorted part, it is compared with ai -1, ai -2 etc. When an element aj with aj <= ai is found, ai is inserted behind it.. If no such element is found, then ai is inserted at the beginning of the sequence.  (^) After inserting element a i the^ length^ of^ the^ sorted^ part^ has increased by one. In the next iteration, ai +1 is inserted into the sorted part etc. While at the beginning the sorted part consists of element a 0 only, at the end it consists of all elements a 0 , ..., an -1.

M5 Lecture Notes – Searching and

Insertion Sort

Analysis

 (^) Worst case: O(n (^2) )  For small arrays  Insertion sort is appropriate due to its simplicity  (^) For large arrays  Insertion sort is prohibitively inefficient

M5 Lecture Notes – Searching and

Quicksort

 To sort a[left...right]:

  1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and all a[p+1...right] are >= a[p] 1.2. Quicksort a[left...p-1] 1.3. Quicksort a[p+1...right]
  2. Terminate

M5 Lecture Notes – Searching and

Partitioning

 Choose an array value (say, the first) to use as the pivot  Starting from the left end, find the first element that is greater than or equal to the pivot  (^) Searching backward from the right end, find the first element that is less than the pivot  Interchange (swap) these two elements  (^) Repeat, searching from where we left off, until done

M5 Lecture Notes – Searching and

Partitioning

 To partition a[left...right]:

  1. Set p = a[left], l = left + 1, r = right;
  2. while l < r, do 2.1. while l < right & a[l] < p, set l = l + 1 2.2. while r > left & a[r] >= p, set r = r - 1 2.3. if l < r, swap a[l] and a[r]
  3. Set a[left] = a[r], a[r] = p
  4. Terminate