































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
































M5 Lecture Notes – Searching and Sort ing 1
M5 Lecture Notes – Searching and
(^) A process that organizes a collection of data into either ascending or descending order
(^) 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
(^) 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
(^) 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
(^) 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
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
(^) 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
A mergesort with an auxiliary temporary array
M5 Lecture Notes – Searching and
(^) 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
(^) 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
(^) 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
To sort a[left...right]:
M5 Lecture Notes – Searching and
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
To partition a[left...right]: