Chapter 6... Tree
Chapter 6... Tree
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.
Linear search,
Binary search,
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.
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.
-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:
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.
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!