Sorting Search New
Sorting Search New
Sorting Search New
SORTING:-
Sorting is the operation of arranging the records of a table into some sequential order
according to an ordering criterion (condition). Sorting is performed on internal & external
memory. It is classified into two categories:
1. Internal Sort: All data items reside in main memory
2. External Sort: This method is very useful when the volume of the data is very large & at
a time can not fit all the data items into a computer memory.
The sort is performed according to the key value of each record. Records can be sorted either
numerically or alphanumerically. In numerical sorting, the records are arranged in ascending or
descending order according to the numerical value of the key.
The complexity of sorting algorithm measures the running time as a function of the number of
items n to be sorted. So the complexity function measures only the number of comparisons.
There are various types of internal sorting: bubble sort, selection sort, insertion sort, heap sort,
radix sort, shell sort, merge sort, and quick sort.
Sorting
Selection Sort
Bubble Sort
Insertion Sort Quick Sort
Divide & Conquer Methods
Heap Sort Merge Sort
Shell Sort
The efficiency of any algorithm can be determined by three different case of a table.
a) Best Case:
The case occurs when sorting techniques have minimum number of comparison occurs.
b) Worst Case:
The case occurs when sorting techniques have maximum number of comparison occurs.
c) Average Case:
The case occurs when sorting techniques have average number of comparison occurs.
1) Selection Sort :-
The selection sort starts from first element & search the entire list until it find the minimum
value of the list then the selection sort will place the minimum value at the first place, then select
the second element & search for the 2nd smallest element from the list & this process will be
continue until the whole list will be sorted. From the algorithm it is clear that the algorithm
consume most of its time in the inner loop during the current pass. Outer loop will be (size-
BMU-BMCCA Page 1 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel
current) & inner loop will be (size-current-1) comparison. Time required for this algorithm is
E(n)=O(n2).
Algorithm:
Selection_sort (a, n)
a = list of element (array)
n = size of list of element
Example: - 44 33 55 22 11
BMU-BMCCA Page 2 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel
2) Bubble Sort :-
The best case involves performing one pass which requires n-1 comparisons the best case is
O(n).
The worst case performance of the bubble sort is n(n-1)/2 comparisons & n(n-1)/2
interchanges.
The average case is more difficult to analyze than the other case. the average case analysis is
O(n2).
Algorithm:
Bubble_Sort (a, n)
a = list of element (array)
n = size of list of element
BMU-BMCCA Page 3 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel
5 2 6 3 9
0 1 2 3 4
There are 5 elements, there will be 3 iterations. In the first iteration a[0] is compared with
a[1],a[1] is compared with a[2],…a[3] is compared with a[4] & each time we interchange the
values if a[j] >a[j+1] i.e. we try to bring the largest value in the array to the last position in
array.
Step 1:
5 2 6 3 9
2 5 6 3 9
2 5 6 3 9
2 5 3 6 9
Step 2:
2 5 3 6 9
2 3 5 6 9
2 3 5 6 9
Step 3:
2 3 5 6 9
Example 2: 80 70 44 55 11 15
Step 1:
70 80 44 55 11 15
70 44 80 55 11 15
70 44 55 80 11 15
70 44 55 11 80 15
70 44 55 11 15 80
Step 2:
44 70 55 11 15 80
44 55 70 11 15 80
44 55 11 70 15 80
44 55 11 15 70 80
Step 3:
44 55 11 15 70 80
44 11 55 15 70 80
44 11 15 55 70 80
Step 4:
11 44 15 55 70 80
11 15 44 55 70 80
Step 5:
11 15 44 55 70 80
BMU-BMCCA Page 4 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel
3) Insertion Sort :-
Insertion sort is also one that sorts a set of records by inserting records into an exiting sorted
file.
Suppose A is the list of element & n is no of elements. We 1st scan all the elements in array
from A[1] to A[n].Inserting each element in its proper position in the previous sorted sub list.
7 2 6 3 9
0 1 2 3 4
There are 5 elements, there will be 3 iterations. In the first iteration a[1] is compared with
a[0]. If a[1] > a[0] ,then no interchange otherwise interchanging will be take place. In second
iteration, a[2] is compared with a[1] & a[1] with a[0]. Again if a[2] > a[1], then no
interchange otherwise interchanging will be take place
Example 1:
7 2 6 3 9 (Comparison R → L)
Step 1:
7 2 6 3 9 (here 2 <7 =True then interchange 2 & 7)
2 7 6 3 9
Step 2:
2 7 6 3 9
2 6 7 3 9
2 6 7 3 9
Step 3:
2 6 7 3 9
2 6 3 7 9
2 3 6 7 9
2 3 6 7 9 It’s sorted array
BMU-BMCCA Page 5 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel
Example 2:
80 70 44 55 11 15
Step 1:
70 80 44 55 11 15
70 44 80 55 11 15
70 44 55 80 11 15
70 44 55 11 80 15
70 44 55 11 15 80
Step 2:
44 70 55 11 15 80
44 55 70 11 15 80
44 55 11 70 15 80
44 55 11 15 70 80
Step 3:
44 55 11 15 70 80
44 11 55 15 70 80
44 11 15 55 70 80
Step 4:
11 44 15 55 70 80
11 15 44 55 70 80
Step 5:
11 15 44 55 70 80
Algorithm:
Insertion_Sort (a, n)
BMU-BMCCA Page 6 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel
4) Quick Sort :-
It is also called partition exchange sort method. When sort is begins, it selects the Lists any
one element which is called pivot element. At each step, the goal is to place a pivot element
in its final position in a list.
The sort then divides the list into two sub lists, one with elements that are less than pivots
elements element and a second list with elements are greater then or equal to pivot elements.
So this technique partition the list into two sub list & repeat until all elements in a list are
placed in their final position. So it is called partition exchange sort method (quick sort).
Example: Consider A as table that contain N elements, chose an element which works as a pivot
element (we can select the first element as pivot element).
For first pass, a pivot elements is set at place j element of list A. where
1) Each element from j-1 is smaller than pivot element.
2) Each element from j+1 is greater than pivot element.
So whole table or list is divided into two sub list, than each separate sub lists are further process
& then set a pivot to sub list & further divide into sub lists, this process can repeat until whole
table is sorted.
The recursive function or algorithm consists of four steps:
1) If there are one or less elements in array to be sorted. Return immediately.
2) Select an element in the array to serve as a “pivot” point.
3) Divide the array into two sub lists-list one is smaller than pivot & second list is grater
than pivot.
4) Recursively repeat the algorithm for both parts of the original array
66 22 77 44 99 55 70 88 L→R
i
66 22 77 44 99 55 70 88 R→L
i j
66 22 77 44 99 55 70 88 R→L
i j
BMU-BMCCA Page 7 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel
66 22 55 44 99 77 70 88 L→R
i j
66 22 55 44 99 77 70 88 R→L
i j
Now the original Key set has been partitioned into sub-tables, namely, the sets {44, 22, 55} &
{99, 77, 70, 88}.The same process can be applied to each of these sets until the table is
completely sorted.
44 22 55 66 99 77 70 88
Algorithm:
Quick_Sort (a, LB, UB)
a = list of elements
LB = lower bound of sub list
UB = upper bound of sub list
Step-1: [Initialize]
flag = 1
Step-2: [Repeat Step-3 to 5]
If(LB < UB) then
{
i = LB
j = UB
pivot = LB
BMU-BMCCA Page 8 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel
5) Merge Sort :-
It requires two sorted table which can be combined to produce a single sorted table. this
process can be accomplished easily by successively selecting the record with the smallest key
occurring in either of the tables & placing the record in a new table, there by creating an
order list.
List A is a list of N elements & List B is a list of M elements that operation that combined
the elements of list A & list B into a single resulting list size is R which represent N+M. This
type of sort is called “Merge Sort”.
BMU-BMCCA Page 9 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel
If the element of list A & list B are in sorted order than we may get resulting list in sorted
array. If the list A & B are not in sorted order we can make them in sorted order & than both
the list can be merge. It is also possible that 1st merging of A & B and then the sort merge
resulting list in the last execution time is less in this O(r) (order of r)= n + m.
Example:
List A:10 5 20 30 40
List B: 20 50 60 7 4 5 6
Both list are in unsorted order so first we can sort both list A & B and then after merger both list.
List R: 4 5 6 7 10 20 30 40 50 60
Algorithm:
Merge_Sort (list a, n, list b, m)
Step-1: [Initialize]
i = 0, j = 0, k = 0
Step-2: [Repeat step-3 to 4]
while (i < n && j < m) (5<5 && 5<7) --F
Step-3: [Compare the value of list a & b and insert into result list of k]
If list a[i] < list b[j] then (5<5) -F
Result[k] = list a[i]
i++;
k++;
Else If list b[j] < list a[i] then 5<5)
Result[k] = list b[j]
j++;
k++;
If list a[i] = = list b[j] then
Result[k] = list a[i]
i++;
j++’
k++;
BMU-BMCCA Page 10 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel
BMU-BMCCA Page 11 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel
SEARCHING:-
Searching refers to the operation of finding the location of given item in a collection of items.
A table or file is a collection of elements, each of which is called a record.
Search operation allows efficient retrieval of specific items from a set of items.
Types of Searching:-
1. Linear or sequential Search
2. Binary Search
3. Index Search
4. Hash Search
1) Linear Search:-
It is known as Sequential search. It is simplest technique for searching an item from unsorted
list.
It scans each entry in sequential manner until the desired item is found.
In other words, search that looks through a list from the top to bottom while checking each item
with key fields for a match is know as a sequential search or linear search.
Algorithm:
Linear_search(a, n, k)
a=list of elements
n=no of elements in the list
k=value to be searched in the list
BMU-BMCCA Page 12 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel
I 0 1 2 3 4 5
0 6 23 39 14 65 18 65 compare with 6
1 6 23 39 14 65 18 65 compare with 23
2 6 23 39 14 65 18 65 compare with 39
3 6 23 39 14 65 18 65 compare with 14
4 6 23 39 14 65 18 65 compare with 65
2) Binary Search:-
The sequential search is very slow. if we have an array of 1000 elements, we must do 1000
comparison in the worse case.
If the array is not sorted, the sequential search is only alternative.
If array is sorted, we can use more efficient algorithm called binary search.
A search for a particular item with a certain key value resembles the search for a name in a table.
The approximate middle entry of the table is located, and its middle value is examined with the
search key value.
If its key value is low, the key value of the middle entry of first half of the table is examined &
the procedure is repeated on the first half until the required item is found.
If its key value is too high, then the key value of middle entry of second half of table is tried &
the producer is repeated on the second half. This process continues until the desired key is found
or the search interval become empty.
The major drawback of binary search is that it assumes that one has direct access to the middle
value in the list or a sub list, this means that the list must be sorted in an array is typical task &
requires a lot of shuffling of elements up & down.
Algorithm:
Binary_search(a, n, k)
a=list of elements
n=no of elements in the list
k=value to be searched in the list
BMU-BMCCA Page 13 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel
0 0 5 2
1 3 5 4
BMU-BMCCA Page 14 of 15
Data Structures UNIT: SORTING & SEARCHING By :Sneha Patel
BMU-BMCCA Page 15 of 15