0% found this document useful (0 votes)
51 views22 pages

Lab 08 Elementary Sorting Algorithms and Searching: Data Structures CS218 Instructor: Anam Qureshi

This document discusses elementary sorting algorithms and searching techniques. It introduces linear and binary search, and covers implementation details of bubble sort, insertion sort, selection sort, and shell sort. Key points include binary search being faster than linear search for lists over 100 elements, and insertion sort generally being faster than bubble sort. The properties of sorting algorithms, like being adaptive, stable, and in-place, are also reviewed.

Uploaded by

Neha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
51 views22 pages

Lab 08 Elementary Sorting Algorithms and Searching: Data Structures CS218 Instructor: Anam Qureshi

This document discusses elementary sorting algorithms and searching techniques. It introduces linear and binary search, and covers implementation details of bubble sort, insertion sort, selection sort, and shell sort. Key points include binary search being faster than linear search for lists over 100 elements, and insertion sort generally being faster than bubble sort. The properties of sorting algorithms, like being adaptive, stable, and in-place, are also reviewed.

Uploaded by

Neha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 22

Lab 08

Elementary Sorting Algorithms and


Searching
Data Structures CS218
Instructor: Anam Qureshi
Overview
• Introduction to Searching and Sorting
• Linear Vs Binary Search
• Bubble Sort and it’s implementation Details
• Insertion Sort and it’s implementation Details
• Selection Sort and it’s implementation Details
• Shell Sort and it’s implementation Details
Searching
Roll No: Name CNIC Address Contact No:
K18-1099 Raheel 1234567 ABC 0333123456
K18-1234 Hanain 7654323 CDE 0331123456
K18-0011 Zawish 7890065 EFG 0332123456
K18-0018 Fasih 5678903 GHI 0311123456
K18-0012 Abdul 3456789 IJK 0313123456
K18-0001 Faisal 8765987 KLM 0323123456
K18-0003 Saad 8767898 MNO 0334123456
Searching Applications
Sorting
Linear vs Binary Search
Linear Search vs Binary Search
• The benefit of binary search over linear search
becomes significant for lists over about 100 elements.
• For smaller lists linear search may be faster because of
the speed of the simple increment compared with the
divisions needed in binary search.
• Binary search requires that the array must be sorted.
• If the array is sorted, binary search is
– 100 times faster for an array of size 1000
– 50000 times faster for an array of size 1000000
• This is the kind of speedup that we care about when
we analyze algorithms
Linear vs Binary Search
Implementation Details of Binary
Search
Sorting Applications
Sorting Algorithm
• Bubble Sort
• Insertion Sort
• Selection Sort
• Shell Sort
• Merge Sort
• Quick Sort
• Radix Sort
• Count Sort
• Heap Sort
• Topological Sort
Bubble Sort
• It is a naïve sorting algorithm
• Performs slow for large number of inputs
• Bubbles out the largest element after every
iteration
• Working principle of Bubble Sort
Implementation Details of Bubble Sort

Algorithm Bubble_Sort(A)
Input: An array of size n
Output: Sorted array
For(i=0 to n-1) do
For(j=0 to n-1-i) do
if(a[j] > a[j+1]) then
Swap(a[j],a[j+1])
end if
end for
End for
Selection Sort
• It is a simple algorithm
• Inefficient for large number of inputs
• Repeatedly finds the minimum element and
insert it into the sorted sub array.
• Working principle of selection sort
Implementation Details of Selection
Sort
Algorithm Selection_Sort(A)
Input: An array of size n
Output: Sorted array
For(i=0 to A.size) do
min_value=i
for(j=i+1 to A.size) do
if(A[j] < A [min_value]) then
min_value= j
end if
end for
swap(A[min_value], A[j])
End for
Insertion Sort
• Insertion sort performs faster than bubble sort
• It works the same way as organization of play
cards
• Pick an element from unsorted list and insert
it at the correct position
• Working principle of insertion sort
Implementation Details of Insertion
Sort
Algorithm Insertion_Sort(A)
Input: An array of size n
Output: Sorted Array
for (j = 1 to A.size) do
temp = A[j]
i = j-1
while (i>=0 && A[i] > temp) do
A[i+1] = A[i]
i = i-1
end while
A[i+1] = temp
End for
Shell Sort
• It is a variation of insertion sort
• It starts sorting pairs of element far apart from
each other
• By the end, the gap is reduced to 1
– It will act like insertion sort algorithm
• Working principle
Implementation Details of Shell Sort
Algorithm Shell_Sort(A)
Input: An array of size n
Output: Sorted Array
for (gap = n/2 ; gap > 0 ; gap/=2) do
for (i= gap ; i < n; i+=1) do
temp= A[i]
for(j=i ; j >= gap && A[j-gap] > temp ; j-=gap) do
A[j] = A[ j - gap]
end for
A[j]= temp
end for
End for
Properties of Sorting Algorithms

Property Description

Adaptive A sort is adaptive if it runs faster on a partially sorted


array

Stable A sort is stable if it preserves the relative order of equal


keys in the database

In-place A sort is in-place when it does not require extra storage


space to sort the elements
Properties of Sorting Algorithms

Algorithm Adaptive Stable In-place


Bubble Sort No Yes Yes
Selection Sort No No Yes
Insertion Sort Yes Yes Yes
Shell Sort Yes No Yes
Summary
• Sorting makes searching easy
• For large number of elements binary search
will outperform as compared to linear search
• Elementary sorting techniques are good for
small scale. However for larger scale some
advanced sorting techniques are required.

You might also like