What Is Searching?: Terminate The Function
What Is Searching?: Terminate The Function
What is Searching?
Searching is a process of finding a value in a list of values. In other words, searching is the process of
locating given value position in a list of values.
Example
Consider the following list of elements and the element to be searched...
1
DATA STRUCTURES UNIT – 3
DATA STRUCTURES UNIT – 3
Example
Consider the following list of elements and the element to be searched...
DATA STRUCTURES UNIT – 3
DATA STRUCTURES UNIT – 3
What is Sorting?
Sorting is the process of arranging a list of elements in a particular order (Ascending or Descending).
Selection Sort
Selection Sort algorithm is used to arrange a list of elements in a particular order (Ascending or
Descending). In selection sort, the first element in the list is selected and it is compared repeatedly
with all the remaining elements in the list. If any element is smaller than the selected element (for
ascending order), then both are swapped so that first position is filled with the smallest element in the
sorted order. Next, we select the element at a second position in the list and it is compared with all the
remaining elements in the list. If any element is smaller than the selected element, then both are
swapped. This procedure is repeated until the entire list is sorted.
Step 1 - Select the first element of the list (i.e., Element at first position in the list).
Step 2 - Compare the selected element with all the other elements in the list.
Step 3 - In every comparison, if any element is found smaller than the selected element (for ascending
order), then both are swapped.
Step 4 - Repeat the same procedure with element in the next position in the list till the entire list is sorted.
Example
DATA STRUCTURES UNIT – 3
DATA STRUCTURES UNIT – 3
Program:
#include<iostream.h
> #include<conio.h>
void selection_sort (int list[],int
n); void main()
{
int n,i;
int
list[30];
clrscr();
cout<<"Enter no of
elements:"; cin>>n;
cout<<"Enter "<<n<<"
numbers:"; for(i=0;i<n;i++)
cin>>list[i];
selection_sort (list,n);
cout<<"After
sorting:\n";
for(i=0;i<n;i++)
cout<<list[i]<<endl;
getch();
}
Output:
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in wrong order.
Example:
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5 > 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm
does not swap them.
Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The
algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Program:
#include<iostream.h>
#include<conio.h>
void bubble_sort(int list[30],int n);
void main()
{
int n,i;
int list[30];
cout<<"Enter No.of elements:";
cin>>n;
cout<<"Enter "<<n<<" numbers:";
for(i=0;i<n;i++)
cin>>list[i];
bubble_sort(list,n);
cout<<"After sorting:\n";
for(i=0;i<n;i++)
cout<<list[i]<<endl;
}
It iterates the input elements by growing the sorted array at each iteration. It compares the
current element with the largest value in the sorted array. If the current element is greater,
then it leaves the element in its place and moves on to the next element else it finds its correct
position in the sorted array and moves it to that position. This is done by shifting all the
elements, which are larger than the current element, in the sorted array to one position ahead.
Explanation
1. Step 1: The second element of an array is compared with the elements that appear before it
(only first element in this case). If the second element is smaller than first element, second
element is inserted in the position of first element. After first step, first two elements of an
array will be sorted.
2. Step 2: The third element of an array is compared with the elements that appears before it
(first and second element). If third element is smaller than first element, it is inserted in the
position of first element. If third element is larger than first element but, smaller than second
element, it is inserted in the position of second element. If third element is larger than both the
elements, it is kept in the position as it is. After second step, first three elements of an array
will be sorted.
3. Step 3: Similarly, the fourth element of an array is compared with the elements that appears
before it (first, second and third element) and the same procedure is applied and that element
is inserted in the proper position. After third step, first four elements of an array will be
sorted.
Another Example:
Since 7 is the first element and has no other element to be compared with, it remains at its
position. Now when moving towards 4, 7 is the largest element in the sorted list and greater
than 4. So, move 4 to its correct position i.e. before 7. Similarly with 5, as 7 (largest element
in the sorted list) is greater than 5, we will move 5 to its correct position. Finally for 2, all the
elements on the left side of 2 (sorted list) are moved one position forward as all elements are
greater than 2 and then 2 is placed in the first position. Finally, the given array will result in a
sorted array.
Insertion Sort Program:
#include<iostream.h>
#include<conio.h>
void insertion_sort ( int a[ ] , int n);
void main()
{
int list[30],n,i;
clrscr();
insertion_sort(list,n);
cout<<"After sorting:\n";
for(i=0;i<n;i++)
cout<<list[i]<<endl;
getch();
}
int temp = a[ i ];
int pos = i;
}
// moving current element to its correct position.
a[ pos ] = temp;
}
}
Output:
Quick Sort
Quick sort is a fast sorting algorithm used to sort a list of elements. Quick sort algorithm is
invented by C. A. R. Hoare.
The quick sort algorithm attempts to separate the list of elements into two parts and then sort
each part recursively. That means it use divide and conquer strategy. In quick sort, the partition
of the list is performed based on the element called pivot. Here pivot element is one of the
elements in the list.
The list is divided into two partitions such that "all elements to the left of pivot are smaller
than the pivot and all elements to the right of pivot are greater than or equal to the pivot".
Example
Merge Sort
Merge sort is one of the most efficient sorting algorithms. It works on the principle of Divide
and Conquer. Merge sort repeatedly breaks down a list into several sublists until each sublist
consists of a single element and merging those sublists in a manner that results into a sorted list.
At the first step this list of size 3 is divided into 2 sublists the first consisting of
elements (9,7) and the second one being (8). Now, the first list consisting of elements (9,7) is
further divided into 2 sublists consisting of elements (9) and (7) respectively.
As no further breakdown of this list can be done, as each sublist consists of a maximum
of 1 element, we now start to merge these lists. The 2 sub-lists formed in the last step are then
merged together in sorted order using the procedure mentioned above leading to a new list (7,9).
Backtracking further, we then need to merge the list consisting of element (8) too with this list,
leading to the new sorted list (7,8,9).