Unit V

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

SEARCHING

It is a technique that helps to find a place of a given element in a list. Any


search option said to successful if element to be search is found. Otherwise search option is
unsuccessful. The standard technics for search options are:

1. Linear Search
This is the simplest searching technique where search on a given array is done by
traversing the array from beginning till the desired element is found. The time
complexity of linear search is order of n where n is number of elements in array.
2. Binary Search
This search technique is done on a list of sorted elements. Here, search operation starts
by comparing the search element with middle element in the list. If search element is
smaller than the middle element then repeat the same process for the left sub list of the
middle element. If search element is larger then, repeat the same process for the right
sub list of the search element. Repeat the process until the search element is found in
the list or until left with a sub list of only one element. If that element does not match
the search element then, the desired element is not found in the list. The time
complexity of binary search is order of log 𝑛.

Q. Implement a function for binary search

void BinarySearch()

int count, i, arr[15], num, first, last, middle;

cout<<"Enter the number of elements";

cin>>count;

for (i=0; i<count; i++)


{

cout<<"Enter number "<<(i+1)<<": ";

cin>>arr[i];

cout<<"Enter the number to search:";

cin>>num;

first = 0;

last = count-1;

middle = (first+last)/2;

while (first <= last)

if(arr[middle] < num)

first = middle + 1;

else if(arr[middle] == num)

cout<<num<<" found in the array at the location "<<middle+1<<"\n";

break;

else {

last = middle - 1;

middle = (first + last)/2;

if(first > last)

cout<<num<<" not found in the array";

}}
SORTING
Sorting refers to operation for arranging set of data in ascending or
descending order according to relations among data items.

Categories of Sorting

1. Internal Sorting
If the data item to be sorted can be adjusted at a time in primary memory then it is
called an internal sorting.

2. External Sorting
When data items that are to be sorted cannot be accommodated to the main memory at
the same time and quotient of data elements have to be kept in secondary memory
then such category of sorting is called as external sorting.

3. Bubble sort
It compares the entire element one by one and sorts them based on their values. If an
array of elements are sorted in ascending order by using bubble sort by comparing first
element of an array to the second if first element is greater than second element to
interchange two elements. The time complexity of bubble sort is in the order of 𝑛2.

4. Quick Sort

QuickSort is a Divide and Conquer algorithm. The time complexity in worst case is O(n2 )
and average case is O(n log n). It picks an element as pivot and partitions the given array
around the picked pivot. There are many different versions of quickSort that pick pivot in
different ways.

1. Always pick first element as pivot.


2. Always pick last element as pivot (implemented below)
3. Pick a random element as pivot.
4. Pick median as pivot.
The key process in quick sort is partition(). Target of partitions is, given an array and an
element x of array as pivot, put x at its correct position in sorted array and put all smaller
elements (smaller than x) before x, and put all greater elements (greater than x) after x. All
this should be done in linear time.
Partition Algorithm:

It starts from the leftmost element and keep track of index of smaller (or equal to)
elements as i. While traversing, if it find a smaller element, then swap current element
with arr[i]. Otherwise ignore current element.
int partition (int arr[], int low, int high)
{
int pivot = arr[high]; // pivot
int i = (low - 1); // Index of smaller element

for (int j = low; j <= high- 1; j++)


{
if (arr[j] <= pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}

void quickSort(int arr[], int low, int high)


{
if (low < high)
{
int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);


quickSort(arr, pi + 1, high);
}
}

/* Function to print an array */


void printArray(int arr[], int
size)
{
int i;
for (i=0; i < size; i++)
cout<<arr[i]<<endl;
}

int main()
{
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr)/sizeof(arr[0]);
quickSort(arr, 0, n-1);
cout<<”Sorted array: “<<endl;
printArray(arr, n);
return 0;
}
Eg:-

arr[] = {10, 80, 30, 90, 40, 50, 70}


Indexes:0 1 2 3 4 5 6

low = 0, high =6, pivot = arr[h] = 70

Initialize index of smaller element, i =-1

Traverse elements from j = low to high-1


j = 0 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])
i=0
arr[] = {10, 80, 30, 90, 40, 50, 70}
// No change as i and j are same
j = 1 : Since arr[j] > pivot, do nothing
// No change in i and arr[]

j = 2 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])

i=1
arr[] = {10, 30, 80, 90, 40, 50, 70} // We swap 80 and 30

j = 3 : Since arr[j] > pivot, do nothing

// No change in i and arr[]

j = 4 : Since arr[j] <= pivot, do i++ and swap(arr[i], arr[j])


i=2
arr[] = {10, 30, 40, 90, 80, 50, 70} // 80 and 40 Swapped
j = 5 : Since arr[j] <= pivot, do i++ and swap arr[i] with arr[j]
i=3
arr[] = {10, 30, 40, 50, 80, 90, 70} // 90 and 50 Swapped

We come out of loop because j is now equal to high-1.


Finally we place pivot at correct position by swapping
arr[i+1] and arr[high] (or pivot)
arr[] = {10, 30, 40, 50, 70, 90, 80} // 80 and 70 Swapped

Now 70 is at its correct place. All elements smaller than


70 are before it and all elements greater than 70 are afte

You might also like