Types of Sorting

You might also like

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

SORTING

Data sorting is any process that involves arranging the


data into some meaningful order to make it easier to
understand, analyze or visualize. When working with
research data, sorting is a common method used for
visualizing data in a form that makes it easier to
comprehend the story the data is telling. Sorting can be
done with raw data (across all records) or at an
aggregated level (in a table, chart, or some other
aggregated or summarized output).

Data is typically sorted based on actual values, counts or


percentages, in either ascending or descending order, but
can also be sorted based on the variable value labels.
Value labels are metadata found in some programs which
allow the researcher to store labels for each value option
of a categorical question. Most software applications also
allow sorting by multiple variables. This type of sorting
will be executed in a predetermined variable priority, for
example, a data set containing region and country fields
can first be sorted by region as the primary sort and then
by country. The county sort will be applied within each
sorted region.

Types of sorting
1.Bubble Sort
2.Selection Sort
3.Insertion Sort
4.Radix Sort
5.Quick Sort
BUBBLE SORT

Bubble Sort is the simplest sorting algorithm that


works by repeatedly swapping the adjacent elements
if they are in the wrong order. This algorithm is not
suitable for large data sets as its average and worst-
case time complexity is quite high.

How does Bubble Sort Work?


Input: arr[] = {5, 1, 4, 2, 8}

First Pass:
Bubble sort starts with very first two
elements, comparing them to check which
one is greater.
( 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:
Now, during second iteration it should look
like this:
( 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 )
Program
Input
#include <stdio.h>
void swap(int *xp, int *yp){
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void bubbleSort(int arr[], int n){
int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
swap(&arr[j], &arr[j + 1]);
}
void printArray(int arr[], int size){
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main(){
int arr[] = {5, 1, 4, 2, 8};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

Output
Sorted array:
12458
Selection sort

The selection sort algorithm sorts an array by repeatedly


finding the minimum element (considering ascending order)
from the unsorted part and putting it at the beginning.

The algorithm maintains two subarrays in a given array.


The subarray which already sorted.
The remaining subarray was unsorted.

In every iteration of the selection sort, the minimum element


(considering ascending order) from the unsorted subarray is
picked and moved to the sorted subarray.

How does Selection Sort Work?


First pass:
For the first position in the sorted array, the whole array is
traversed from index 0 to 4 sequentially. The first position where
64 is stored presently, after traversing whole array it is clear that
11 is the lowest value.
64 25 12 22 11
Thus, replace 64 with 11. After one iteration 11, which happens to
be the least value in the array, tends to appear in the first position
of the sorted list.
11 25 12 22 64

Second Pass:
For the second position, where 25 is present, again traverse the
rest of the array in a sequential manner.
11 25 12 22 64

After traversing, we found that 12 is the second lowest value in the


array and it should appear at the second place in the array, thus
swap these values.
11 12 25 22 64
Program
Input
#include <stdio.h>
void swap(int *xp, int *yp){
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void selectionSort(int arr[], int n){
int i, j, min_idx;
for (i = 0; i < n-1; i++){
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
if(min_idx != i)
swap(&arr[min_idx], &arr[i]);
}
}
void printArray(int arr[], int size){
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main(){
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

Output
Sorted array:
11 12 22 25 64
Insertion Sort

Insertion sort is a simple sorting algorithm that works similar


to the way you sort playing cards in your hands. The array is
virtually split into a sorted and an unsorted part. Values from
the unsorted part are picked and placed at the correct
position in the sorted part.

Characteristics of Insertion Sort:


This algorithm is one of the simplest algorithm
with simple implementation
Basically, Insertion sort is efficient for small data
values
Insertion sort is adaptive in nature, i.e. it is
appropriate for data sets which are already
partially sorted.

First Pass:
Initially, the first two elements of the array are compared in
insertion sort.
12 11 13 5 6
Here, 12 is greater than 11 hence they are not in the ascending
order and 12 is not at its correct position. Thus, swap 11 and 12.
So, for now 11 is stored in a sorted sub-array.

11 12 13 5 6
Second Pass:
Now, move to the next two elements and compare them

11 12 13 5 6
Here, 13 is greater than 12, thus both elements seems to be in
ascending order, hence, no swapping will occur. 12 also stored in a
sorted sub-array along with 11
Program
Input
#include <math.h>
#include <stdio.h>
void insertionSort(int arr[], int n){
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n){
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
5 6 11 12 13
}
int main(){
int arr[] = { 12, 11, 13, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printArray(arr, n);
return 0;
}

Output

5 6 11 12 13

You might also like