Sorting and Searching DS
Sorting and Searching DS
Sorting and Searching DS
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.
Algorithm
The selection sort algorithm is performed using the following steps...
• 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 comparision, 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
Quick sort
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.
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".
• Step 1 - Assume that first element in the list is in sorted portion and all the remaining
elements are in unsorted portion.
• Step 2: Take first element from the unsorted portion and insert that element into the sorted
portion in the order specified.
• Step 3: Repeat the above process until all the elements from the unsorted portion are moved
into the sorted portion.
Example
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in wrong order.
Bubble sort algorithm repeatedly compares the adjacent elements and swaps them if not in order.
1. Starting from the first index, compare the first and the second elements.
2. If the first element is greater than the second element, they are swapped.
3. Now, compare the second and the third elements. Swap them if they are not in order. The
above process goes on until the last element.
5. After each iteration, the largest element among the unsorted elements is placed at the end.
Merge Sort
Merge Sort is one of the most popular sorting algorithms that is based on the principle of Divide
and Conquer Algorithm.
Here, a problem is divided into multiple sub-problems. Each sub-problem is solved individually.
Finally, sub-problems are combined to form the final solution.
Using the Divide and Conquer technique, we divide a problem into subproblems. When the
solution to each subproblem is ready, we 'combine' the results from the subproblems to solve the
main problem.
Algorithm
Merge sort keeps on dividing the list into equal halves until it can no more be divided. By
definition, if it is only one element in the list, it is sorted. Then, merge sort combines the smaller
sorted lists keeping the new list sorted too.
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
Example
• Merge sort may access data in order, thus random access isn’t required.
• The merge sort requires a similar size array to sort the list, which is a disadvantage of the
memory use.
What is Search?
Search 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.
Repeat the same until search element is compared with the last element in the list, if that last
element also doesn't match, then the result is "Element not found in the list".
That means, the search element is compared with element by element in the list.
Algorithm
Linear search is implemented using following steps.
Step 2 - Compare the search element with the first element in the list.
Step 3 - If both are matched, then display "Given element is found!!!" and terminate the function
Step 4 - If both are not matched, then compare search element with the next element in the list.
Step 5 - Repeat steps 3 and 4 until search element is compared with last element in the list.
Step 6 - If last element in the list also doesn't match, then display "Element is not found!!!" and
terminate the function.
Example:
Binary Search
Algorithm
Binary search is implemented using following steps.
For all the sorting and searching methods refer lab programs