3. Sorting
Sorting is the process of placing elements from a
collection in some kind of order.
Types
Bubble Sort.
Insertion Sort.
Selection Sort.
Quick Sort.
Merge Sort.
4. Bubble sort
Bubble sort is a simple sorting algorithm. This sorting
algorithm is comparison-based algorithm in which each
pair of adjacent elements is compared and the elements
are swapped if they are not in order.
7. Insertion Sort
Insertion sort is a sorting algorithm in which the elements are
transferred one at a time to the right position by comparison
but with left values.
This is an in-place comparison-based sorting
Algorithm.
13. Selection Sort
In selection sort, the smallest element is selected from the
unsorted array and swapped with the leftmost element,
and that element becomes a part of the sorted array. This
process continues moving unsorted array boundary by one
element to the right.
Step 1 − Set MIN to location 0.
Step 2 − Search the minimum element in the list.
Step 3 − Swap with value at location MIN.
Step 4 − Increment MIN to point to next element.
Step 5 − Repeat until list is sorted.
16. Merge Sort
Merge sort is a sorting technique based on divide and
conquer technique.
Merge sort first divides the array into equal halves and
then combines them in a sorted manner.
23. void Merge(int *a, int low, int high, int mid)
{// We have low to mid and mid+1 to high already
sorted.
int i, j, k, temp[high-low+1];
i = low;
k = 0;
j = mid + 1;
// Merge the two parts into temp[].
while (i <= mid && j <= high)
{ if (a[i] < a[j])
{ temp[k] = a[i];
k++; i++; }
else
{ temp[k] = a[j];
k++; j++; }}
// Insert all the remaining values from i to mid
into temp[].
while (i <= mid)
{ temp[k] = a[i];
k++; i++; }
// Insert all the remaining values from j to high
into temp[].
while (j <= high)
{ temp[k] = a[j];
k++;j++;} // Assign sorted data stored in temp[] to
a[].
for (i = low; i <= high; i++)
{ a[i] = temp[i-low]; } }
Merge(a, low, high, mid); [array,1,2,1]
i = low=1; k = 0; j = mid + 1=2;
// Merge the two parts into temp[].
while (i <= mid && j <= high)
{ if (a[i] < a[j])
{ temp[k] = a[i];
k++; i++; }
else
{ temp[k] = a[j];
k++; j++; }}
while (i <= mid)
{ temp[k] = a[i];
k++; i++; }
// Insert all the remaining values from j to
high into temp[].
while (j <= high)
{ temp[k] = a[j];
k++;j++;}
while (j <= high)
{ temp[k] = a[j];
k++;j++;}
// Assign sorted data stored in temp[] to a[].
for (i = low; i <= high; i++)
{ a[i] = temp[i-low]; } }
Merge(a, low, high, mid); [array,3,4,3]
i = low=3; k = 0; j = mid + 1=3;
// Merge the two parts into temp[].
while (i <= mid && j <= high)
{ if (a[i] < a[j])
{ temp[k] = a[i];
k++; i++; }
else
{ temp[k] = a[j];
k++; j++; }}
while (i <= mid)
{ temp[k] = a[i];
k++; i++; }
// Insert all the remaining values from j to
high into temp[].
while (j <= high)
{ temp[k] = a[j];
k++;j++;}
while (j <= high)
{ temp[k] = a[j];
k++;j++;}
// Assign sorted data stored in temp[] to a[].
for (i = low; i <= high; i++)
{ a[i] = temp[i-low]; } }
Merge(a, low, high, mid); [array,1,4,2]
i = low=1; k = 0; j = mid + 1=3;
// Merge the two parts into temp[].
while (i <= mid && j <= high)
{ if (a[i] < a[j])
{ temp[k] = a[i];
k++; i++; }
else
{ temp[k] = a[j];
k++; j++; }}
while (i <= mid)
{ temp[k] = a[i];
k++; i++; }
// Insert all the remaining values from j to
high into temp[].
while (j <= high)
{ temp[k] = a[j];
k++;j++;}
while (j <= high)
{ temp[k] = a[j];
k++;j++;}
// Assign sorted data stored in temp[] to a[].
for (i = low; i <= high; i++)
{ a[i] = temp[i-low]; } }
24. #include <iostream>
using namespace std;
// A function to merge the two half into a sorted data.
void Merge(int *a, int low, int high, int mid)
{
// We have low to mid and mid+1 to high already sorted.
int i, j, k, temp[high-low+1];
i = low;
k = 0;
j = mid + 1;
// Merge the two parts into temp[].
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
temp[k] = a[i];
k++;
i++;
}
else
{
temp[k] = a[j];
k++;
j++;
}
}
// Insert all the remaining values from i to mid into temp[].
while (i <= mid)
{
temp[k] = a[i];
k++;
i++;
}
// Insert all the remaining values from j to high into temp[].
while (j <= high)
{
temp[k] = a[j];
k++;
j++;
}
// Assign sorted data stored in temp[] to a[].
for (i = low; i <= high; i++)
{
a[i] = temp[i-low];
}
}