Sort Algo by Dr. Arun Parakh
Sort Algo by Dr. Arun Parakh
int main()
{
int arr[] = { 64, 34,2)5, 12), 2)2), 11, 90 };
int n = sizeof(arr) / sizeof(arr[0]);}
bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}
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.
1) The subarray which already sor ted.
2)) The remaining subarray was unsor ted.
●
In every iteration of the selection sort, the minimum
element (considering ascendingorder) from the unsorted
subarray is picked and moved to the sorted subarray.
Selection sort
Follow the below steps to solve the problem:
●
Initialize minimum value(min_idx) to location 0.
●
Traverse the array to find the minimum element in the
array.
●
While traversing if any element smaller than min_idx is
found then swap both the
●
values.
●
Then, increment min_idx to point to the next element.
●
Repeat until the array is sor ted.
Selection sort
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
merge(arr, l, m, r);
}
}
Merge Sort
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2) = r - m;
References:
●
https://www.geeksforgeeks.org
●