Sorting Algorithms, Exams of Advanced Algorithms

Slides on different sorting algorithms such as Insertion Sort, Merge Sort, Quick Sort, and Counting Sort. It includes pseudo code and illustrative examples of how each algorithm works. Insertion Sort is typically done in-place, by iterating up the array, growing the sorted list behind it. Merge Sort divides the array in half and recursively sorts each half before merging them. Quick Sort divides the array into two sub-arrays and recursively sorts them. Counting Sort creates a count array to store count of individual elements and then modifies the input array.

Typology: Exams

2022/2023

Available from 03/29/2023

ClemBSC
ClemBSC 🇺🇸

3.8

(32)

1.6K documents

1 / 64

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Sorting Algorithms
Slide 2-3 – Insertion Sort
Slides 4-44 – Merge Sort along with illustrative example
Slides 46-48 – Quick Sort along with illustrative example
Slides 49-64 – Counting sort along with illustrative example
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40

Partial preview of the text

Download Sorting Algorithms and more Exams Advanced Algorithms in PDF only on Docsity!

Sorting Algorithms

  • (^) Slide 2-3 – Insertion Sort
  • (^) Slides 4-44 – Merge Sort along with illustrative example
  • (^) Slides 46-48 – Quick Sort along with illustrative example
  • (^) Slides 49-64 – Counting sort along with illustrative example

Insertion Sort

for(int i=1; i<=length-1; i++) int j=i; while(j>0 and A(j-1) > A(j) ) swap A(j-1) and A(j) j=j- end while end for On your left, is the pseudo code for insertion sort. Below is an example of how this sorting technique works with actual array elements. Sorting is typically done in- place, by iterating up the array, growing the sorted list behind it.

Merge Sort

A divide-and-conquer algorithm :

  • (^) Divide the unsorted array into 2 halves until the sub-arrays only contain one element
  • (^) Merge the sub-problem solutions together:
    • (^) Compare the sub-array’s first elements
    • (^) Remove the smallest element and put it into the result array
    • (^) Continue the process until all elements have been put into the result array

Merge Sort continued

Mergesort(Pass an array) if array size > 1 Divide array in half Call Mergesort on first half. Call Mergesort on second half. Merge two halves. Merge(Pass two arrays) Compare leading element in each array Select lower and place in new array. (If one input array is empty then place remainder of other array in output array) MergeSort(A, left, right) { if (left < right) { mid = floor((left + right) / 2); MergeSort(A, left, mid); MergeSort(A, mid+1, right); Merge(A, left, mid, right); } } // Merge() takes two sorted subarrays of A and // merges them into a single sorted subarray of A // (how long should this take?)

Merge

Merge

Merge

Merge

Merge

Merge

Merge