Docsity

Binary Search & Sorting Algorithms: Bubble, Insertion, Selection, QuickSort, MergeSort, Study notes of Compilers

Partial preview of the text

Download Binary Search & Sorting Algorithms: Bubble, Insertion, Selection, QuickSort, MergeSort and more Study notes Compilers in PDF only on Docsity!

Arrays, Vectors

Searching, Sorting

Arrays

char s[200]; //array of 200 characters

  • different type than class^ string can be accessed as s[0], s[1], ..., s[199]
  • s[0]=’H’; s[1]=’e’; s[2]=’l’; s[3]=’l’; s[4]=’o’;
  • char a = s[3]; works for any type
  • double d[10]; int i[100]; C++ does not check for array bounds !!

Memory allocation for arrays

int A[n]//allocates n*size(int) bytes 21 -56 0 -1 109 4 5 11 -3 - 0 1 2 3 4 n-2 n- 4 bytes 4 bytes 4 bytes 4 bytes 4 bytes 4 bytes 4 bytes 4 bytes 4 bytes 4 bytes indices values memory

sizeof(int)

0 1 2 3 4 n-2 n- 8 bytes 8 bytes 8 bytes 8 bytes 8 bytes 8 bytes 8 bytes 8 bytes 8 bytes 8 bytes indices values memory

sizeof(double)

Array initialization

complete

  • int A[5]={0, 1, 10, 100, 1000} partial
  • int A[5] = {0,1,10}^ //allocates int[5]
  • only indices 0,1,2 are initialized with values implicit size
  • int A[] = {0,1,-1,2}^ //allocates an int[4]

Array copy

int A[5] = {1,2,3,4,5} int B[5]; B=A; //error instead copy each element

  • for (int i=0;i<5;i++) B[i] = A[i];

Parallel arrays (DB tables)

requires "join" operations

ID NAME

0 Virgil 1 Alex 2 Bob 3 Cindy 4 n

ID AGE

n

ID GENDER
0 M
1 F
2 M
3 F

n

ID School Status

0 PhD 1 in College 2 HighSchool 3 PhD Candidate 4 n

Array bounds

C++ does not check for array bounds !! very easy to read/write at the wrong location memory address

  • writing particularly bad : can overwrite a variable

Searching

Sorting

Binary Search Efficiency

every iteration/recursion

  • ends the procedure if value is found
  • if not, reduces the problem size (search space) by half worst case : value is not found until problem size=
  • how many reductions have been done?
  • n / 2 / 2 / 2 /.... / 2 = 1. How many 2-s do I need?
  • if k 2-s, then n= 2 k, so k is about log(n)
  • worst running time is O(log n)

Search: tree of comparisons

compare compare compare compare compare compare compare compare compare compare compare compare compare tree of comparisons : essentially what the algorithm does

Search: tree of comparisons

tree of comparisons : essentially what the algorithm does

  • each program execution follows a certain^ path

compare compare compare compare compare compare compare compare compare compare compare compare compare

Search: tree of comparisons

tree of comparisons : essentially what the algorithm does

  • each program execution follows a certain^ path
  • red^ nodes are terminal / output
  • the algorithm has to have at least n output nodes... why?

compare compare compare compare compare compare compare compare compare compare compare compare compare

Insertion Sort

partial array is sorted get a new element V=

Insertion Sort

partial array is sorted get a new element V= find correct position with binary search i=

Insertion Sort

partial array is sorted get a new element V= find correct position with binary search i= move elements to make space for the new element

Insertion Sort

partial array is sorted get a new element V= find correct position with binary search i= move elements to make space for the new element insert into the existing array at correct position

Selection Sort

sort array A[] into a new array C[] while (condition)

  • find^ minimum^ element x in A at index i, ignore "used" elements
  • write x in next available position in C
  • mark index i in A as "used" so it doesn't get picked up again Running Time = O(n 2 )

✘ - ✘ - 12

  • 9

used A C

Selection Sort

sort array A[] into a new array C[] while (condition)

  • find^ minimum^ element x in A at index i, ignore "used" elements
  • write x in next available position in C
  • mark index i in A as "used" so it doesn't get picked up again Running Time = O(n 2 )

✘ - ✘ - 12 ✘ - 9

used A C

Selection Sort

sort array A[] into a new array C[] while (condition)

  • find^ minimum^ element x in A at index i, ignore "used" elements
  • write x in next available position in C
  • mark index i in A as "used" so it doesn't get picked up again Running Time = O(n 2 )

✘ - ✘ - 12 ✘ - ✘ 9

used A C

Selection Sort

sort array A[] into a new array C[] while (condition)

  • find^ minimum^ element x in A at index i, ignore "used" elements
  • write x in next available position in C
  • mark index i in A as "used" so it doesn't get picked up again Running Time = O(n 2 ) ✘ 10 ✘ - ✘ - 12 ✘ - ✘ 9

used A C

QuickSort Partition

TASK: rearrange A and find pivot q, such that

  • all elements before q are smaller than A[q]
  • all elements after q are bigger than A[q] Partition (A, b, e)
  • x=A[e]//pivot value
  • i=b-
  • for j=b TO e-
  • if A[j]<=x then
  • i++; swap A[i]<->A[j]
  • swap A[i+1]<->A[e]
  • q=i+1; return q

Partition Example

set pivot value x = A[e], // x=

  • i =index of last value < x
  • i+1 = index of first value > x run j through array indices b to e-
  • if A[j] <= x^ //see steps (d),(e) swap (A[j] , A[i+1]); i++; //advance i move pivot in the right place
  • swap (pivot=A[e] , A[i+1]) return pivot index
  • return i+

QuickSort time

Depends on the Partition balance Worst case: Partition produces unbalanced split n = (1, n-1) most of the time

  • results in O(n (^2) ) running time Average case: most of the time split balance is not worse than n = (cn, (1-c)n) for a fixed c
  • for example c=0.99 means balance not worse than (1/100n, 99/100n)
  • results in O(n*log(n)) running time

Merge two sorted arrays

two sorted arrays

  • A[] = { 1, 5, 10, 100, 200, 300}; B[] = {2, 5, 6, 10}; merge them into a new array C
  • index i for array A[], j for B[], k for C[]
  • init i=j=k=0;
  • while (what_condition_?) if (A[i] <= B[j]) { C[k]=A[i], i++ } //advance i in A else {C[k]=B[j], j++} // advance j in B advance k
  • end_while

Sorting : tree of comparisons

tree of comparisons : essentially what the algorithm does

  • each program execution follows a certain^ path
  • red^ nodes are terminal / output
  • the algorithm has to have n! output nodes... why?
  • if tree is balanced, longest^ path^ = tree depth = n log(n)
  • if tree not balanced, path can be longer compare compare compare compare compare compare compare compare compare compare compare compare compare

tree

depth

Linear-time Sorting

Counting Sort (A[]) : count values, NO comparisons STEP 1 : build array C that counts A values

  • init C[]=0 ; run index i through A value = A[i] C[value] ++; //counts each value occurrence STEP 2: build array D of positions
  • init total =0; run index i through C D[i] = total; total += C[i]; STEP3: assign values to output array E
  • run index i through A value = A[i]; position = D[i]; E[position] = value;

Two Dim Arrays,

Vectors,

Basic Hashing

Two dimensional array = matrix

double M[10][20]; //matrix of real numbers allocates 1020sizeof(double) = 1600 bytes as function parameter: must specify the # of columns

  • int myfunction (double X[][20], int rows) double M[2][5]; //allocates 80 bytes: 0.11 -8.6 102 -1.8 9.75 14 1.25 101 -3.1 39.

8 bytes 8 bytes 8 bytes 8 bytes 8 bytes 8 bytes 8 bytes 8 bytes 8 bytes 8 bytes indices values memory

sizeof(double)