You are on page 1of 39

Chapter Six

Prepared by Sino
Searching and
Sorting Algorithms
Introduction:
 Sorting is one of the most common tasks
performed by the computer, and used:-
 to arrange data in meaningful way.

 To improve efficiency of searching Algorithms.

There are different types of sorting algorithms in


data structure. Such as:-
A. Elementary sorting algorithms
 Insertion, selection, bubble
B. Advanced sorting algorithms
 Quick, shell, heap, merge, hash and radix
Coun’t
Factor considered to select sorting algorithm to
used are:
 The complexity of algorithm
 The size of data structure to be sorted

 The time programmer takes to understand and


implement and so on.
Searching:
 Searching algorithms are used to read a particular
record from a collection of records.
 Different searching algorithms are used for
searching for sorted or unsorted records.
 Some of the searching algorithms are

Linear search,

Binary search,

Depth first search and so on.


Sequential and binary Searching
 Searching is a process of looking for specific
element in a list of items or determining that the
item is not in the list.
 There are two simple, searching algorithms:
 Sequential Searching algorithms
 Binary search

 The method we use depends on how the elements


of the list are organized.

 unordered list: linear search: simple, slow


 an ordered list: binary search complex, faster
Sequential/Linear search
 Proceeds by sequentially comparing the key with
elements in the list
 Continues until either we find a match or the end of
the list is encountered.
 If we find a match, the search terminates
successfully by returning the index of the element
 If the end of the list is encountered without a
match, the search terminates unsuccessfully.

 The computational time for this algorithm is


proportional to n. Therefore the time complexity is
O(n)
Implementation
int linear-Search(int list[], int n, int key)
{
int index=0;
int found=0;
do{
if(key==list[index])
found=1;
else
index++;
}
while(found==0)&&index<n)
if(found==0)
index=-1;
return index; }
Binary Search
 List must be a sorted one
 We compare the element with the element placed
approximately in the middle of the list
 If a match is found, the search terminates
successfully.
 Otherwise, we continue the search for the key in a
similar manner either in the upper half or the lower
half.
 Works only on an ordered list. the basic idea is:
 The computational time for this algorithm is
proportional to log2n. Therefore the time
complexity is O(log n).
Implementation
int Binary Search(int list[], int k)
Lb=mid+1;
{ }
int Lb=0; }while(found==0&&Lb<Ub);
int Ub=n-1; if(found==0)
int found=0; index=-1;
else
do{
index=mid;
mid=(Lb+Ub)/2; return index;
if(key==list[mid]) }
found=1;
else{
if(key<list[mid])
Ub=mid-1;
else
Coun’t…
 Binary search relies on the ability to access
components directly by subscripting the
array.
 No such facility is available with the linked
list because access is sequential, starting from
the first item.
 So that, linked lists are not easily searched
using this method (binary search).
Elementary Sorting Algorithms
Sorting:- is the process of reordering a list of items
in either increasing or decreasing order.
 Sorting algorithm can be categorized as simple and
advanced.
 the following are some of simple/elementary
sorting algorithm.
 Insertion Sort, Selected Sort, Bubble sort
 Simple sorting algorithms used (better)to sort with
a small amount of data.
 To measure the efficiency of sorting algorithm
critical properties such as: the number of
comparisons and the number of data movements
made are used.
Sorting: Example
Generally:
 It is simple and applicable in a small size of items but, if the
item is so large it is not applicable
 Strings, characters are complex but integers are easy to sort.

 Suppose :-there is an order relation that can be set across the


elements
 Goal :-Arrange the elements in ascending order
 Start  1 23 2 56 9 8 10 100
 End  1 2 8 9 10 23 56 100

How?
Running time
Insertion Sort
 divide the collection of items into two lists, one
listed with one element (sorted) and the other with
the remaining elements (unsorted).
 On successive passes take an item from the unsorted
list and insert it into the sorted list so the sorted list
is always sorted.
 Do this until the unsorted list is empty.

Variations of Insertion Sort


 Shifting down with swaps

 Shifting with out swaps


Example of Insertion Sort using
“Shifting down” using swaps

1 3 7 9 16 5

1 3 7 9 5 16

1 3 7 5 9 16

1 3 5 7 9 16
Example of Insertion Sort using
““Shifting Instead of swapping”

1 3 7 9 16 5

1 3 7 9 ? 16

1 3 7 ? 9 16

1 3 5 7 9 16
Coun’t …
 There are two major modifications of insertion sort:
Linear and binary. They can be distinguished by the
method of finding a place to insert the current element.
Using Binary Search
 it is reasonable to use binary search algorithm to find a
proper place for insertion. This variant of the insertion
is called binary insertion sort. After position for
insertion is found, algorithm shifts the part of the array
and inserts the element.
 Advantage:
 uses the last number of comparisons then that Bubble
sort and selection sort, so it might be fast.
Coun’t …
Disadvantage:
 It does not work well for reversely items.

 Always the first element is assumed sorted

 Element may already be in their proper positions


can be moved from these positions and then later
moved back.
 If an item is being inserted, all elements greater
than the one being inserted has to be moved.
insertion is not localized and may require moving
a significant number of elements.
Complexity Analysis
 Insertion sort’s overall complexity is O(n2) on
average, regardless of the method of insertion .
 but number comparisons may vary depending on
the insertion algorithm.
 it is O(n2) when shifting or swapping methods
are used .
 and O(n logn) for binary insertion sort.
Implementation
void insertionSort(int arr[], int n){
int j, key;
for(int i = 1; i < n; i++){
key = arr[i];
j = i - 1;
while(j >= 0 && arr[j] > key)
{ arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
2. Selection Sort
 Make a pass across the data looking for the largest
item, swap the largest with the last item in the array.
 On successive passes (n-1) assume the array is one
smaller (the last item is in the correct place) and
repeat previous step. or
 Array is imaginary divided into two parts. Sorted one
and unsorted one.
 At the beginning, sorted part is empty, while unsorted
one contains whole array.
 At every step, algorithm finds minimal element in the
unsorted part and adds it to the end of sorted one.
 When unsorted part becomes empty, algorithm stops.
Example: Sort {5, 1, 12, -5, 16, 2, 12, 12, 14}
using selection sort
5 1 12 -5 16 2 12 14
5 1 12 -5 16 2 12 14
-5 1 12 5 16 2 12 14

-5 1 12 5 16 2 12 14
-5 1 2 5 16 12 12 14

-5 1 2 5 16 12 12 14

-5 1 2 5 12 16 12 14
-5 1 2 5 12 12 16 14
-5 1 2 5 12 12 14 16
Implementation
void selection_sort(int arr[], int n)
{int i, j, min;
for (i = 0; i < n - 1; i++)
{
min = i;
for (j = i+1; j < n; j++)
{ if (list[j] < list[min]) min = j; }
swap(arr[i], arr[min]);
}
}
Complexity Analysis
 The analysis of the performance of the function
selection sort() is simplified by the presence of two
for loops with lower and upper bounds. the outer
executes n-1 times, and for each I between o and n-2.
Bubble Sort
 Simplest sorting algorithm.
 Bubble sort belongs to O(n2) sorting
algorithms, which makes it quit efficient for
sorting large data volumes.
 Bubble sort is stable and adaptive. Bubble
sort is adaptive means that for almost
sorted array it gives O(n) estimation.
 Bubble is the simplest algorithm to
implement and the slowest algorithm on
very large inputs.
Algorithm:
 Compare each pair of adjacent elements from
the beginning of an array and, if they are in
reversed order, swap them. If at least one
swaps one swaps has been done, repeat step 1.
 For the first iteration push the largest element
at the last. Then the next iteration caparison
will be done up to the node before the last. and
the remaining iteration proceed the same way
until the last iteration with only one
comparison, data[0] with data[1], and possibly
one interchange are performed.
Generally:

 arrange the elements of the list by forming


pairs of adjacent elements.

 The pair of the ith and (i+1)th element.


 If the order is ascending, we interchange the
elements of the pair

 This will bring the highest value from


among the remaining (n−1) values to the
(n−1)th position.
Example: Sort {5,1,12,-5, 16} using bubble sort

5 1 12 -5 16

5 1 12 -5 16
1 5 12 -5 16
1 5 12 -5 16
1 5 -5 12 16

1 5 -5 12 16
1 5 -5 12 16
1 -5 5 12 16

1 -5 5 12 16
-5 1 5 12 16

-5 1 5 12 16
-5 1 5 12 16
N.B. Implementation part of a bubble sort tray your self.
 The number of comparisons is the same in each case(best,
average, and worst) and equals the total number of
iterations of the inner for loop.

 So the time complexity of bubble sort is O(n2)


Advanced Sorting and Searching
Algorithms
 There are different types of advanced sorting
algorithms.
Such as:
 Shell sort

 Merge sort

 Quick sort

 Heap sort

 Radix sort
 Hash sort and son.
Shell Sort algorithm
 Shell sort is the oldest fast sorting algorithms which
is an improvement of insertion sort.
 It is developed by Donald shell in 1959. it is fast,
easy to understand and easy to implement. However,
its complexity analysis is a little more sophisticated.
The idea of shell sort is the following:
 Arrange the data sequence in a two-dimensional
array
 sort the columns of the array. The effect is that the
data sequence is partially sorted.
Coun’t …
 The process above is repeated, but each time with a
narrower array, i.e. with a smaller number of columns.
in the last step, the array consists of only one column.
 in each step, the sortedness of the sequence is increased,
until in the last step it is completely sorted.
 However, the number of sorting operations necessary in
each step is limited, due to the presortedness of the
sequence obtained in the preceding steps
 Time complexity of shell sort is O(n3/2)
H1=1
Hi+1=3*hi+1
 An Important property of shell sort
 A sequence which is hk sorted that is then hk-
1-sorted will remain hk sorted. this means that
work done by early phases is not undone by
later phases
 The action of an hk sorted is to perform an
insertion sort on hk independent sub arrays
 shell sort is a non stable in place sort.
Example:
Example: sort the following list using shell sort algorithm.

5 8 2 4 1 3 9 7 6 0
Thank You!

You might also like