0% found this document useful (0 votes)
14 views9 pages

Quick Sort,Merge Sort.... Algorithm

This document provides an overview of various sorting algorithms including Insertion Sort, Quick Sort, Merge Sort, and Radix Sort. It details the working procedure, advantages, and step-by-step processes for each algorithm, emphasizing the simplicity and efficiency of Insertion Sort for small data sets, while also noting its limitations compared to other algorithms. The document serves as a helpful resource for students preparing for examinations on sorting algorithms.

Uploaded by

mdistiaqahmmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views9 pages

Quick Sort,Merge Sort.... Algorithm

This document provides an overview of various sorting algorithms including Insertion Sort, Quick Sort, Merge Sort, and Radix Sort. It details the working procedure, advantages, and step-by-step processes for each algorithm, emphasizing the simplicity and efficiency of Insertion Sort for small data sets, while also noting its limitations compared to other algorithms. The document serves as a helpful resource for students preparing for examinations on sorting algorithms.

Uploaded by

mdistiaqahmmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Insertion Sort Algorithm

In this article, we will discuss the Insertion sort Algorithm. The working procedure of insertion sort
is also simple. This article will be very helpful and interesting to students as they might face
insertion sort as a question in their examinations. So, it is important to discuss the topic.

Insertion sort works similar to the sorting of playing cards in hands. It is assumed that the first
card is already sorted in the card game, and then we select an unsorted card. If the selected
unsorted card is greater than the first card, it will be placed at the right side; otherwise, it will be
placed at the left side. Similarly, all unsorted cards are taken and put in their exact place.

The same approach is applied in insertion sort. The idea behind the insertion sort is that first take
one element, iterate it through the sorted array. Although it is simple to use, it is not appropriate
for large data sets as the time complexity of insertion sort in the average case and worst case
is O(n2), where n is the number of items. Insertion sort is less efficient than the other sorting
algorithms like heap sort, quick sort, merge sort, etc.

Insertion sort has various advantages such as -

o Simple implementation
o Efficient for small data sets
o Adaptive, i.e., it is appropriate for data sets that are already substantially sorted.

Now, let's see the algorithm of insertion sort.

Algorithm
The simple steps of achieving the insertion sort are listed as follows -

Step 1 - If the element is the first element, assume that it is already sorted. Return 1.

Step2 - Pick the next element, and store it separately in a key.

Step3 - Now, compare the key with all elements in the sorted array.

Step 4 - If the element in the sorted array is smaller than the current element, then move to the
next element. Else, shift greater elements in the array towards the right.

Step 5 - Insert the value.

Step 6 - Repeat until the array is sorted.

Working of Insertion sort Algorithm


Now, let's see the working of the insertion sort Algorithm.
To understand the working of the insertion sort algorithm, let's take an unsorted array. It will be
easier to understand the insertion sort via an example.

Let the elements of array are -

Initially, the first two elements are compared in insertion sort.

Here, 31 is greater than 12. That means both elements are already in ascending order. So, for
now, 12 is stored in a sorted sub-array.

Now, move to the next two elements and compare them.

Here, 25 is smaller than 31. So, 31 is not at correct position. Now, swap 31 with 25. Along with
swapping, insertion sort will also check it with all elements in the sorted array.

For now, the sorted array has only one element, i.e. 12. So, 25 is greater than 12. Hence, the
sorted array remains sorted after swapping.

Now, two elements in the sorted array are 12 and 25. Move forward to the next elements that are
31 and 8.

Both 31 and 8 are not sorted. So, swap them.


After swapping, elements 25 and 8 are unsorted.

So, swap them.

Now, elements 12 and 8 are unsorted.

So, swap them too.

Now, the sorted array has three items that are 8, 12 and 25. Move to the next items that are 31
and 32.

Hence, they are already sorted. Now, the sorted array includes 8, 12, 25 and 31.

Move to the next elements that are 32 and 17.

17 is smaller than 32. So, swap them.

Swapping makes 31 and 17 unsorted. So, swap them too.


Now, swapping makes 25 and 17 unsorted. So, perform swapping again.

Quick sort Algorithm:


Quick Sort is also called partition-exchange sort. Quick Sort is a divide and conquer algorithm. A quick sort
first selects a value, which is called the pivot value.
It creates two empty arrays to hold elements less than the pivot value and elements greater than the
pivot value, and then recursively sort the sub arrays. There are two basic operations in the algorithm,
swapping items in place and partitioning a section of the array.

Pivot element can be chosen from the given numbers in many different ways:
Choosing the first element
Choosing the last element (example shown)
Choosing any random element
Choosing the median

Quick Sort Algorithm: Steps on how it works:


1. Find a “pivot” item in the array. This item is the basis for comparison for a single round.
2. Start a pointer (the left pointer) at the first item in the array.
3. Start a pointer (the right pointer) at the last item in the array.
4. While the value at the left pointer in the array is less than the pivot value, move the left pointer to the
right (add 1). Continue until the value at the left pointer is greater than or equal to the pivot value.
5. While the value at the right pointer in the array is greater than the pivot value, move the right pointer
to the left (subtract 1). Continue until the value at the right pointer is less than or equal to the pivot
value.
6. If the left pointer is less than or equal to the right pointer, then swap the values at these locations in
the array.
7. Move the left pointer to the right by one and the right pointer to the left by one.
8. If the left pointer and right pointer don’t meet, go to step 1.

Or

Step by Step Process


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
Merge Sort :

Merge sort is a famous sorting algorithm.


 It uses a divide and conquer paradigm for sorting.
 It divides the problem into sub problems and solves them individually.
 It then combines the results of sub problems to get the solution of the original problem.

The merge sort algorithm is an implementation of the divide and conquer technique. Thus, it gets completed
in three steps:

1. Divide: In this step, the array/list divides itself recursively into sub-arrays until the base case is reached.
2. Recursively solve: Here, the sub-arrays are sorted using recursion.
3. Combine: This step makes use of the merge( ) function to combine the sub-arrays into the final sorted
array.
Algorithm for Merge Sort
Step 1: Find the middle index of the array.
Middle = 1 + (last – first)/2 or (first +last)/2
Step 2: Divide the array from the middle.
Step 3: Call merge sort for the first half of the array
Merge Sort(array, first, middle)
Step 4: Call merge sort for the second half of the array.
Merge Sort(array, middle+1, last)
Step 5: Merge the two sorted halves into a single sorted array.

Example:

How Merge Sort Works?


To understand merge sort, we take an unsorted array as the following −

We know that merge sort first divides the whole array iteratively into equal halves unless the atomic values are
achieved. We see here that an array of 8 items is divided into two arrays of size 4.

This does not change the sequence of appearance of items in the original. Now we divide these two arrays into halves.

We further divide these arrays and we achieve atomic value which can no more be divided.
Now, we combine them in exactly the same manner as they were broken down. Please note the color codes given to
these lists.
We first compare the element for each list and then combine them into another list in a sorted manner. We see that 14
and 33 are in sorted positions. We compare 27 and 10 and in the target list of 2 values we put 10 first, followed by 27.
We change the order of 19 and 35 whereas 42 and 44 are placed sequentially.

In the next iteration of the combining phase, we compare lists of two data values, and merge them into a list of found
data values placing all in a sorted order.

After the final merging, the list should look like this −

Example 2:
Radix sort:
Radix sort is a non-comparison-based sorting algorithm. The word radix, by definition, refers to the base or the number
of unique digits used to represent numbers. In radix sort, we sort the elements by processing them in multiple passes,
digit by digit.

Each pass sorts elements according to the digits in a particular place value, using a stable sorting algorithm (usually
counting sort as a subroutine). It can be implemented to start the sorting from the least significant digit (LSD) or the
most significant digit (MSD).

Step by Step Process


The Radix sort algorithm is performed using the following steps...

 Step 1 - Define 10 queues each representing a bucket for each digit from 0 to 9.
 Step 2 - Consider the least significant digit of each number in the list which is to be sorted.
 Step 3 - Insert each number into their respective queue based on the least significant digit.
 Step 4 - Group all the numbers from queue 0 to queue 9 in the order they have inserted into their respective queues.
 Step 5 - Repeat from step 3 based on the next least significant digit.
 Step 6 - Repeat from step 2 until all the numbers are grouped based on the most significant digit.

You might also like