Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Sorting and Searching, Slides of Data Structures and Algorithms

Includes Bubble sort, Insertion Sort etc

Typology: Slides

2018/2019

Uploaded on 03/31/2019

AnjanaAshok
AnjanaAshok 🇮🇳

3 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
SORTING AND SEARCHING
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Sorting and Searching and more Slides Data Structures and Algorithms in PDF only on Docsity!

SORTING AND SEARCHING

Bubble Sort

● Starting with the first element(index = 0), compare the current

element with the next element of the array.

● If the current element is greater than the next element of the

array, swap them.

● If the current element is less than the next element, move to the

next element. Repeat Step 1.

Complexity of Bubble Sort

● In Bubble Sort, n-1 comparisons will be done in the 1st pass, n-

in 2nd pass, n-3 in 3rd pass and so on. So the total number of

comparisons will be,

f(n) = (n-1) + (n-2) + (n-3) + ..... + 3 + 2 + 1

f(n) = n(n-1)/

i.e O(n 2 )

Selection Sort

● algorithm will first find the smallest element in the array and

swap it with the element in the first position, then it will find the

second smallest element and swap it with the element in the

second position, and it will keep on doing this until the entire

array is sorted.

● It is called selection sort because it repeatedly selects the

next-smallest element and swaps it into the right place.

Selection Sort

SMALLEST (ARR, K, N, POS) SELECTION SORT(ARR, N)

Step 1: [INITIALIZE] SET SMALL = ARR[K] Step 1: Repeat Steps 2 and 3 for K = 0 to N- Step 2: [INITIALIZE] SET POS = K Step 2: CALL SMALLEST(ARR, K, N,POS) Step 3: Repeat for J = K+1 to N -1 Step 3: SWAP A[K] with ARR[POS] IF SMALL > ARR[J] [END OF LOOP] SET SMALL = ARR[J] Step 4: Exit SET POS = J [END OF IF] [END OF LOOP] Step 4: RETURN POS

Example

Insertion Sort

● Insertion sort is a simple sorting algorithm that works the way

we sort playing cards in our hands

INSERTION-SORT (ARR, N)

Step 1: Repeat Steps 2 to 5 for K = 1 to N – 1

Step 2: SET TEMP = ARR[K]

Step 3: SET J = K - 1

Step 4: Repeat while TEMP <= ARR[J] and J>= SET ARR[J + 1] = ARR[J] SET J = J - 1 [END OF INNER LOOP] Step 5: SET ARR[J + 1] = TEMP

[END OF LOOP]

Step 6: EXIT

Merge Sort

● Merge sort is a sorting algorithm that uses the divide, conquer, and combine algorithmic paradigm. ● Divide means partitioning the n -element array to be sorted into two sub-arrays of n/2 elements. If A is an array containing zero or one element, then it is already sorted. However, if there are more elements in the array, divide A into two sub-arrays, A 1 and A 2 , each containing about half of the elements of A. ● Conquer means sorting the two sub-arrays recursively using merge sort. ● Combine means merging the two sorted sub-arrays of size n/2 to produce the sorted array of n elements.

Algorithm for Merge Sort

MERGE_SORT(ARR, P, R)

Step 1: IF P < R

SET Q = (P + R)/

MERGE_SORT (ARR, P, Q)

MERGE_SORT (ARR, Q + 1, R)

MERGE (ARR, P, Q, R)

[END OF IF]

Step 2: END

Complexity of Merge Sort

The list of size N is divided into a max of log N parts, and the

merging of all sublists into a single list takes O(N) time, the worst

case run time of this algorithm is O(N log N)

Quick Sort

The quick sort algorithm works as follows:

  1. Select an element pivot from the array elements.
  2. Rearrange the elements in the array in such a way that all elements that are less than the pivot appear before the pivot and all elements greater than the pivot element come after it (equal values can go either way). After such a partitioning, the pivot is placed in its final position.

This is called the partition operation.

  1. Recursively sort the two sub-arrays thus obtained. (One with sub-list of values smaller than that of the pivot element and the other having higher value elements.)

PARTITION (ARR, P, R)

X = ARR[R]

i = P - 1 For j = P to R - 1 { If ( ARR[ j ] <= X ) { i = i + 1 Exchange ARR[ i ] with ARR[ j ] } } Exchange ARR[ i + 1] with ARR[ R ] Return i + 1

Complexity of Quick Sort

● In the best case, every time we partition the array, we divide the

list into two nearly equal pieces. That is, the recursive call

processes the sub-array of half the size. At the most, only log n

nested calls can be made before we reach a sub-array of size 1. It

means the depth of the call tree is O(log n). And because at

each level, there can only be O(n) , the resultant time is given as

O(nlog n) time.