•Sorting
needs to speed up searching operation in a list.
•Sorting type:
–Ascending
–Descending
Sorting
algorithm:
1.
Internal sorting
All data to be sorted are loaded to RAM
2.
External sorting
Sorting process using secondary storage
•Simple:
– Bubble
sort
– Selection
sort
– Insertion
sort
•Intermediate:
– Quick
Sort
– Merge
Sort
BUBBLE SORT
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in wrong order.
•Compare two neighboring values.
•Compare and swap (if necessary)
•Also known as exchange sort
•Source Code of Bubble Sort:
void Bubble(int *DataArr, int n)
{
int i, j;
for(i=1;
i<n; i++)
for(j=n-1;
j>=i; j--)
if(DataArr[j-1]
> DataArr[j])
Swap(&DataArr[j-1],&DataArr[j]);
}
SELECTION SORT
Selection sort in C: C program for selection sort to sort numbers. This code implements selection sort algorithm to arrange numbers of an array in ascending order. With a little modification, it will arrange numbers in descending order.
Algorithm:
for(i=0;
i<N-1;
i++){ /* N=number of data */
Set idx_smallest
equal to i
for(j=i+1;
j<N; j++){
If array[ j ] < array [ idx_smallest ]
then idx_smallest = j
}
Swap array[ i ]
with array[ idx_smallest ]
}
INSERTION SORT
Insertion sort is a simple sorting algorithm that works the way we sort playing cards in our hands.
Algorithm
// Sort an arr[] of size n
insertionSort(arr, n)
Loop from i = 1 to n-1.
……a) Pick element arr[i] and insert it into sorted sequence arr[0…i-1]
// Sort an arr[] of size n
insertionSort(arr, n)
Loop from i = 1 to n-1.
……a) Pick element arr[i] and insert it into sorted sequence arr[0…i-1]
QUICK SORT
Like Merge Sort, QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of quickSort that pick pivot in different ways.
- Always pick first element as pivot.
- Always pick last element as pivot (implemented below)
- Pick a random element as pivot.
- Pick median as pivot.
The key process in quickSort is partition(). Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x. All this should be done in linear time.
Algorithm:
void
QuickSort(int left, int right)
{
if(left < right){
//arrange elements R[left],...,R[right] that
//producing new sequence:
R[left],...,R[J-1] < R[J] and
R[J+1],...,R[right] > R[J].
QuickSort(left, J-1);
QuickSort(J+1, right);
}
}
MERGE SORT
Like QuickSort, Merge Sort is a Divide and Conquer algorithm. It divides input array in two halves, calls itself for the two halves and then merges the two sorted halves. The merge() function is used for merging two halves. The merge(arr, l, m, r) is key process that assumes that arr[l..m] and arr[m+1..r] are sorted and merges the two sorted sub-arrays into one.
•Merge Sort is a sorting algorithm based on the divide-and-conquer algorithm
•Divide-and-conquer is a general algorithm design paradigm
–Divide: divide the input data in two disjoint subsets
–Recur: solve the sub problems associated with subsets
–Conquer: combine the solutions for each subset into a solution
No comments:
Post a Comment