Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Sorting and Searching DS

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

Sorting

Sorting is the process of arranging a list of elements in a particular order.

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 by Step Process (algorithm)


In Quick sort algorithm, partitioning of the list is performed using following steps.
Step 1 - Consider the first element of the list as pivot (i.e., Element at first position in the
list).
Step 2 - Define two variables i and j. Set i and j to first and last elements of the list
respectively.
Step 3 - Increment i until list[i] > pivot then stop.
Step 4 - Decrement j until list[j] < pivot then stop.
Step 5 - If i < j then exchange list[i] and list[j].
Step 6 - Repeat steps 3,4 & 5 until i > j.
Step 7 - Exchange the pivot element with list[j] element.
Program: Refer lab program
Insertion sort
Insertion sort is a sorting algorithm that places an unsorted element at its suitable place in each
iteration.
In insertion sort, there are two lists in the array at every point: A partially sorted list and an unsorted
list. Thus, we virtually split the array into sorted and unsorted parts.
Every time we pick up an element from the unsorted array and place it in the partially sorted array.
Thus, we insert the elements from an unsorted array at their correct position in the sorted array.
That is why the name Insertion. It is not suitable for very large datasets.
Insertion sort algorithm arranges a list of elements in a particular order. In insertion sort algorithm,
every iteration moves an element from unsorted portion to sorted portion until all the elements are
sorted in the list.
Step by Step Process (algorithm)
The insertion sort algorithm is performed using the following steps.

• 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.

Working of Bubble Sort(Algorithm)

Suppose we are trying to sort the elements in ascending 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.

4. The same process goes on for the remaining iterations.

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 1 − if it is only one element in the list it is already sorted, return.

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

Advantages of the Merge Sort Algorithm:

• Merge sorting makes it simple to sort big data sets.

• Merge sort may access data in order, thus random access isn’t required.

• Merge sort is a reliable sorting method.

Disadvantages of the Merge Sort Algorithm:

• The merge sort requires a similar size array to sort the list, which is a disadvantage of the
memory use.

• When sorting smaller data sets, it takes longer.


Searching

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.

Linear Search Algorithm (Sequential Search Algorithm)


Linear search algorithm finds a given element in a list of elements. This search process starts
comparing search element with the first element in the list. If both are matched then result is
element found otherwise search element is compared with the next element in the list.

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 1 - Read the search element from the user.

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

Binary search algorithm finds a given element in a list of


elements. The binary search algorithm can be used with only
a sorted list of elements. That means the binary search is used
only with a list of elements that are already arranged in an
order. The binary search can not be used for a list of elements
arranged in random order. This search process starts
comparing the search element with the middle element in the
list. If both are matched, then the result is "element found".
Otherwise, we check whether the search element is smaller or
larger than the middle element in the list. If the search
element is smaller, then we repeat the same process for the
left sublist of the middle element. If the search element is
larger, then we repeat the same process for the right sublist
of the middle element.

We repeat this process until we find the search element in the


list or until we left with a sublist of only one element. And if
that element also doesn't match with the search element,
then the result is"Element not found in the list".

Algorithm
Binary search is implemented using following steps.

1. Step 1 - Read the search element from the user.

2. Step 2 - Find the middle element in the sorted list.


3. Step 3 - Compare the search element with the
middle element in the sorted list.

4. Step 4 - If both are matched, then display "Given


element is found!!!" and terminate the function.

5. Step 5 - If both are not matched, then check whether


the search element is smaller or larger than the
middle element.

6. Step 6 - If the search element is smaller than middle


element, repeat steps 2, 3, 4 and 5 for the left sublist
of the middle element.

7. Step 7 - If the search element is larger than middle


element, repeat steps 2, 3, 4 and 5 for the right
sublist of the middle element.

8. Step 8 - Repeat the same process until we find the


search element in the list or until sublist contains
only one element.

9. Step 9 - If that element also doesn't match with the


search element, then display "Element is not found in
the list!!!" and terminate the function.

Advantages of Binary Search Algorithm

10. Since it follows the technique to eliminate half of


the array elements, it is more efficient as compared
to linear search for large data.
11. Better time complexity and thus takes less
compilation time.

12. An improvement over linear search as it breaks the


array down in half rather than sequentially traversing
through the array elements.

Limitations of Binary Search Algorithm

1. Binary Search algorithm could only be implemented


over a sorted array.

2. Small unsorted arrays would take considerate time


in sorting and then searching the desired element.
So, binary search is not preferred in such cases.

3. It has poor locality of reference compared to linear


search algorithm when comes to in-memory
searching for short intervals.
Example: click on below link
https://blog.penjee.com/wp-content/uploads/2015/04/binary-and-linear-search-animations.gif

For all the sorting and searching methods refer lab programs

You might also like