Unit 6 Sorting Algorithms Modified
Unit 6 Sorting Algorithms Modified
1.In Pass 1, A[0] is compared with A[1], A[1] is compared with A[2], A[2]
is compared with A[3] and so on. At the end of pass 1, the largest element
of the list is placed at the highest index of the list.
2.In Pass 2, A[0] is compared with A[1], A[1] is compared with A[2] and
so on. At the end of Pass 2 the second largest element of the list is placed
at the second highest index of the list.
3.In pass n-1, A[0] is compared with A[1], A[1] is compared with A[2] and
so on. At the end of this pass. The smallest element of the list is placed at
the first index of the list.
Algorithm Bubble Sort:
•Step 1: Repeat Step 2 For i = 0 to N-1
•Step 2: Repeat For j = 0 to N - i
•Step 3: IF A[j+1] > A[j]
SWAP A[j+1]
and A[j]
[END OF INNER
LOOP]
[END OF OUTER LOOP
•Step 4: EXIT
#include<stdio.h>
void main () Output:
{
int i, j,temp; Printing Sorted Element
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
int n =10;
List . . .
for(i = 0; i<10; i++) 7
{ 9
for(j = 0; j<(n-i); j++) 10
{
if(a[j+1] > a[j]) 12
{ 23
temp = a[j]; 34
a[j] = a[j+1];
a[j+1] = temp;
34
} 44
} 78
}
101
printf("Printing Sorted Element List ...\n");
for(i = 0; i<10; i++)
{
printf("%d\n",a[i]);
}
}
Insertion Sort
Insertion sort is the simple sorting algorithm which
is commonly used in the daily lives while ordering
a deck of cards. In this algorithm, we insert each
element onto its proper place in the sorted array.
This is less efficient than the other sort algorithms
like quick sort, merge sort, etc.
Technique
Consider an array A whose elements are to be
sorted. Initially, A[0] is the only element on the
sorted set. In pass 1, A[1] is placed at its proper
index in the array.
In pass 2, A[2] is placed at its proper index in the
array. Likewise, in pass n-1, A[n-1] is placed at its
proper index into the array.
To insert an element A[k] to its proper index, we
must compare it with all other elements i.e. A[k-
1], A[k-2], and so on until we find an element A[j]
•In 2nd pas, position pos of the smallest element present in the
sub-array A[n-1] is found. Then, swap, A[1] and A[pos]. Thus
A[0] and A[1] are sorted, we now left with n-2 unsorted
elements.
•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 4: Repeat the same procedure with element in the next position
in the list till the entire list is sorted.
#include <stdio.h>
int main()
{
int a[100], n, i, j, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d Numbersn", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for(i = 0; i < n - 1; i++)
{
position=i;
for(j = i + 1; j < n; j++)
{
if(a[position] > a[j])
position=j;
}
if(position != i)
{
swap=a[i];
a[i]=a[position];
a[position=swap;
}
}
printf("Sorted Array:n");
for(i = 0; i < n; i++)
printf("%dn", a[i]);
return 0;
}
Merge sort
Merge sort is the algorithm which follows divide and conquer approach.
Consider an array A of n number of elements. The algorithm processes the
elements in 3 steps.
2.Conquer means sort the two sub-arrays recursively using the merge sort.
3.Combine the sub-arrays to form a single final sorted array maintaining the
ordering of the array.
The main idea behind merge sort is that, the short list takes less time to be
sorted.
Consider the following array of 7 elements.
Sort the array by using merge sort.
1.A = {10, 5, 2, 23, 45, 21, 7}
Algorithm •IF I > MID
•Step 1: [INITIALIZE] SET I = Repeat while J <= END
SET TEMP[INDEX] = ARR[J]
BEG, J = MID + 1, INDEX = 0
SET INDEX = INDEX + 1, SET J = J + 1
•Step 2: Repeat while (I <= MID)
[END OF LOOP]
AND (J<=END) [Copy the remaining elements of
IF ARR[I] < ARR[J] left sub-array, if any]
SET TEMP[INDEX] = ARR[I] ELSE
SET I = I + 1 Repeat while I <= MID
ELSE SET TEMP[INDEX] = ARR[I]
SET TEMP[INDEX] = ARR[J] SET INDEX = INDEX + 1, SET I = I + 1
[END OF LOOP]
SET J = J + 1
[END OF IF]
[END OF IF] •Step 4: [Copy the contents of TEMP back to
SET INDEX = INDEX + 1 ARR] SET K = 0
[END OF LOOP] •Step 5: Repeat while K < INDEX
Step 3: [Copy the remaining SET ARR[K] = TEMP[K]
elements of right sub-array, if SET K = K + 1
any] [END OF LOOP]
•Step 6: Exit
MERGE_SORT(ARR, BEG, END)
Radix sort processes the elements the same way in which the names of the
students are sorted according to their alphabetical order. There are 26 radix in
that case due to the fact that, there are 26 alphabets in English.
In the first pass, the names are grouped according to the ascending order of
the first letter of names.
In the second pass, the names are grouped according to the ascending order
of the second letter.
The same process continues until we find the sorted list of names. The bucket
are used to store the names produced in each pass. The number of passes
depends upon the length of the name with the maximum letter.
In the case of integers, radix sort sorts the numbers according to
their digits. The comparisons are made among the digits of the
number from LSB to MSB. The number of passes depend upon the
length of the number with the most number of digit
Example
Consider the array of length 6 given below.
Sort the array by using Radix sort.
• Step
1:Find the largest number in ARR as LARGE
• Step
2: [INITIALIZE] SET NOP = Number of digits in LARGE
• Step
3: Now, create d buckets of size 0 - 9 , where d = NOP
• Step
4: for i -> 0 to d
sort the array elements using counting sort (or any stable
sort) according to the digits at the ith place
• Step 5: END
Algorithm (Counting Sort in Radix Sort)
Algorithm (Radix Sort Implementation)
// Radix sort Function
void radixsort(int arr[], int n)
{
int m = getMax(arr, n);
// Do counting sort for every digit.
for (int exp = 1; m / exp > 0; exp *= 10)
countSort(arr, n, exp);
}
In the first loop, n is equal to 8 (size of the array), so the elements are lying at the
interval of 4 (n/2 = 4). Elements will be compared and swapped if they are not in
order.
Example continue…
Now, we are taking the interval of 2 to sort the rest of the array. With an interval of
2, two sublists will be generated - {12, 25, 33, 40}, and {17, 8, 31, 42}.