0% found this document useful (0 votes)
3 views11 pages

Sorting Techniques

The document provides an overview of various sorting algorithms, including Quick Sort, Heap Sort, and Merge Sort, detailing their mechanisms and implementations. Quick Sort uses a pivot to partition the list, while Heap Sort builds a heap structure to sort elements. Merge Sort is highlighted for external sorting, particularly when dealing with large datasets, and employs a divide and conquer strategy to merge sorted subarrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views11 pages

Sorting Techniques

The document provides an overview of various sorting algorithms, including Quick Sort, Heap Sort, and Merge Sort, detailing their mechanisms and implementations. Quick Sort uses a pivot to partition the list, while Heap Sort builds a heap structure to sort elements. Merge Sort is highlighted for external sorting, particularly when dealing with large datasets, and employs a divide and conquer strategy to merge sorted subarrays.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

The Quick Sort

The quick sort uses divide and conquer approach to sort the list.A quick sort first selects a
value, which is called the pivot value. Although there are many different ways to choose the
pivot value, we will simply use the first item in the list. The role of the pivot value is to assist
with splitting the list. The actual position where the pivot value belongs in the final sorted list,
commonly called the split point, will be used to divide the list for subsequent calls to the quick
sort.
Following figure shows that 54 will serve as our first pivot value. The partition process will
happen next. It will find the split point and at the same time move other items to the appropriate
side of the list, either less than or greater than the pivot value.

Partitioning begins by locating two position markers-at the beginning and end of the remaining
items in the list ( leftmark and rightmark respectively). The goal of the partition process is to
move items that are on the wrong side with respect to the pivot value while also converging on
the split point.
We begin by incrementing leftmark until we locate a value that is greater than the pivot value.
We then decrement rightmark until we find a value that is less than the pivot value. At this
point we have discovered two items that are out of place with respect to the eventual split point.
For our example, this occurs at 93 and 20. Now we can exchange these two items and then
repeat the process again.

At the point where rightmark becomes less than leftmark, we stop. The position of rightmark
is now the split point. The pivot value can be exchanged with the contents of the split point and
the pivot value is now in place . In addition, all the items to the left of the split point are less
than the pivot value, and all the items to the right of the split point are greater than the pivot
value. The list can now be divided at the split point and the quick sort can be invoked
recursively on the two halves.
Quick Sort:
#include<stdio.h>
void quicksort(int a[25],int first,int last){
int i, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(a[i]<=a[pivot]&&i<last)
i++;
while(a[j]>a[pivot])
j--;
if(i<j){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
quicksort(a,first,j-1);
quicksort(a,j+1,last);
}
}
int main(){
int i, count, a[25];
printf("How many elements are u going to enter?: ");
scanf("%d",&count);
printf("Enter %d elements: ", count);
for(i=0;i<count;i++)
scanf("%d",&a[i]);
quicksort(a,0,count-1);
printf("Order of Sorted elements: ");
for(i=0;i<count;i++)
printf(" %d",a[i]);
return 0;
}
OUTPUT:
HEAP SORT

Heap sort performs sorting on the elements by creating the min-heap or max-heap using the
elements of the given array. Min-heap or max-heap represents the ordering of array in which
the root element represents the minimum or maximum element of the array.The concept of
heap sort is to eliminate the elements one by one from the heap part of the list, and then insert
them into the sorted part of the list.

Heap sort is a comparison-based sorting technique based on Binary Heap data


structure. The algorithm is as follows:
• Build a heap from the given input array.
• Repeat the following steps until the heap contains only one element:
✓ Swap the root element of the heap (which is the largest element) with the last
element of the heap.
✓ Remove the last element of the heap (which is now in the correct position).
✓ Heapify the remaining elements of the heap.
• The sorted array is obtained by reversing the order of the elements in the input array.
#include <stdio.h>
void heapify(int a[], int n, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && a[left] > a[largest])


largest = left;
if (right < n && a[right] > a[largest])
largest = right;
if (largest != i)
{
int temp = a[i];
a[i] = a[largest];
a[largest] = temp;

heapify(a, n, largest);
}
}

void heapSort(int a[], int n)


{
for (int i = n / 2 - 1; i >= 0; i--)
heapify(a, n, i);
for (int i = n - 1; i >= 0; i--)
{
int temp = a[0];
a[0] = a[i];
a[i] = temp;

heapify(a, i, 0);
}
}
int main()
{
int a[20],n,i;
printf("Enter the no. of elements for sorting");
scanf("%d",&n);
printf("\nEnter the elements");
for ( i = 0; i < n; ++i)
scanf("%d", &a[i]);
heapSort(a, n);
printf("\nAfter sorting array elements are - \n");
for (i = 0; i < n; ++i)
{
printf("%d", a[i]);
printf(" ");
}
return 0;
}

OUTPUT:

External Sorting:
When the amount of data is particularly large, due to the limited memory capacity, it is
impossible to sort all the data in memory at one time, so it is necessary to use external sorting.
Merge sort is the algorithm most commonly used for external sorting. External merge sorting
is generally divided into two phases: run formation phase (see figure 1) and run merge phase
(see figure 2). Run formation phase means the formation of initial runs. Replacement selection
is a popular run formation method. Run merge phase means the merge of the runs into a sorted
file.
Run formation phase generally has two modes: load-sort-store and replacement selection. In
load-sort-store mode, the input data is loaded into memory and sorted, and the sorted data is
written to the external memory device to form a run. Replacement selection has the following
advantages over load-sort-store algorithm. First, it can generate longer runs, averagely
generating runs twice the size of memory, thereby reducing the number of runs generated and
the number of merge passes.

Merge Sort:
Merge sort is similar to the quick sort algorithm as it uses the divide and conquer approach to
sort the elements. It is one of the most popular and efficient sorting algorithm.
Merge sort is a recursive algorithm that continuously splits the array in half until it
cannot be further divided i.e., the array has only one element left (an array with one element
is always sorted). Then the sorted subarrays are merged into one sorted array.
Consider a list of numbers 6, 5,12, 10,9, 1 for sorting using merge sort. The following figure
shows how the merge sort splits the arrays and sorts it while merging it back:

#include<stdio.h>
#include<conio.h>
void mergesort(int a[], int low, int high);
void merge(int a[ ],int low,int mid,int high);
void main()
{
int a[20],n,i;
printf("\nEnter the no. of elements:");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("\nSorted elements:");
for(i=0;i<n;i++)
printf(" %d",a[i]);

}
void mergesort(int a[], int low, int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,mid,high);
}
else
return;
}
void merge(int a[ ],int low,int mid,int high)
{
int i,j,k,l,m,b[20];
i=low;
j=mid+1;
k=low;
while(i<=mid && j<=high)
{
if(a[i]<a[j])
{
b[k]=a[i];
i++;
}
else
{
b[k]=a[j];
j++;
}
k++;
}
while(i<=mid)
{
b[k]=a[i];
i++;
k++;
}
while(j<=high)
{
b[k]=a[j];
j++;
k++;
}
for(m=low;m<=high;m++)
a[m]=b[m];
}
OUTPUT:

You might also like