Searching and Sorting Algorithm
Searching and Sorting Algorithm
Lecture_Module 11 : CSL101
Searching Techniques
Information retrieval is one of the most important application of computer Given a name, retrieve associated information Dictionary/directory look-up
Types of Searching
Sequential Searching Can search from unordered list Time complexity O(n) Binary searching Can search from ordered list only Time complexity O(log2n) Tenary searching Can search from ordered list only Time complexity O(log3n) Index searching Can search from ordered list only Uses index table
[1, n/3] ; [n/3 +1, 2n/3] and [2n/3 +1, n] Find out the range in which the key belongs and search that range using ternary search.
Indices
1 2 3 4 5 6 7 8 9
10
12
14
20
24 O(log3n)
List
Complexity
Indexed Search
2 7 14 Index Table
10 12 14 20 24
List
10 16 23 29 37
Index
10
12
14
16
18
20
23
26
29
32
35
37
38
40
List
Sorting Algorithms
Sorting is an important concept in real life. Always better to keep things in some sensible order. Useful for searching Various Techniques for sorting
Selection sort Insertion sort Bubble sort
Selection Sort
Algorithm for arranging elements of list in ascending order.
In unordered list choose minimum and swap with first element of unsorted list. Reduce the list size by one Repeat the process till list reduces to 0 size.
Minimum - Algorithm
Statements for finding minimum of list x(i),x(n) min = x(i); j=i {# j is index at which min resides} k=i+1 while k n { if x(k) < min then { min = x(k); j=k } k=k+1 }
Swap - statements
Swap the values of x(i) and x(j). Here x(i) is the first element of unsorted list x(j) is minimum element
t = x(i) = x(j) = x(i) x(j) t
def min(s, i, j): min=s[i]; index = i for k in range (i+1,j): if min > s[k]: min=s[k] index = k return index >>> s= [8,7,3,4] List at each iteration [3,7,8,4] List at each iteration [3,4,8,7] List at each iteration [3,4,7,8]
Insertion Sort
At each iteration From unordered list choose the first element and insert it in the sorted. Reduce the list size by one Repeat the process till list reduces to 0 size.
Insert - Algorithm
Statements for inserting x(i) in the list x(1),x(i-1) k = i-1; val = x(i) while val < x(k) { x(k+1) = x(k) k=k-1 } x(k+1) = val
Bubble Sort
In each iteration, exchange adjacent elements if not in proper order The highest element gets settled at the bottom of the list. Each time list is reduced by one from start to end1) Stop the process
If no exchange is done OR the list reduces to 1 size.
>>> s= [5,4,8,2 1] List at each iteration [4,5,2,1,8] List at each iteration [4,2,1,5,8] List at each iteration [2,1,4,5,8] List at each iteration [1,2,4,5,8]
Partitioning of a List
Partitioning a given unordered list into two subsets such that
all elements in one set x and other elements in other list are > x
Applications of Partitioning
Sorting Median finding Statistical classification
Exchange wrongly partitioned pair Start the process from the existing positions till i crosses j.
Example
x=11
Initially i=1; j=9
10 20 8 25 6 35 14 26 i (10<x) j
Example Contd
10 20 8 25 6 35 14 26 i (20>x) j (6 < x) 10 6 8 25 20 35 14 26 i (20>x) j (6 < x) 10 6 8 25 20 35 14 26 j i
Exchange i and j position values
Partitioning Algorithm
Input list L(i), i=1,n i=1; j=n While (i j)
{ While (L(i) < x) { i=i+1 } While (L(j) > x) { j=j-1 } Swap L(i) and L(j) values i=i+1 j=j-1 }
Output L(k), k=1,j and L(k), k=i to n