Você está na página 1de 13

Data Structures & Algorithms / Data Structures

Hamdard University

LAB SESSION 09
SORTING ALGORITHMS: HEAP SORT, QUICK SORT AND MERGE SORT
Objectives:
To understand the basic concepts of heap sort, quick sort and merge sort.
To understand the implementations of heap sort, quick sort and merge sort.
Heap sort
Heap sort is a comparison-based sorting algorithm to create a sorted array (or list), and is part of
the selection sort family. Although somewhat slower in practice on most machines than a well-
implemented quicksort, it has the advantage of a more favorable worst-case O(n log n) runtime.
Working of Heap Sort:
In the first step, a heap is built out of the data. The heap is often placed in an array with the layout
of a complete binary tree. The complete binary tree maps the binary tree structure into the array
indices; each array index represents a node; the index of the node's parent, left child branch, or
right child branch are simple expressions. For a zero-based array, the root node is stored at index 0;
if i is the index of the current node, then In the second step, a sorted array is created by repeatedly
removing the largest element from the heap (the root of the heap), and inserting it into the array.
The heap is updated after each removal to maintain the heap. Once all objects have been removed
from the heap, the result is a sorted array.
Heapsort can be performed in place. The array can be split into two parts, the sorted array and the
heap. The storage of heaps as arrays is diagrammed here. The heap's invariant is preserved after
each extraction, so the only cost is that of extraction.
Algorithm
MAX-HEAPIFY(A,i)
l = LEFT(i)
r = RIGHT(i)

Data Structures & Algorithms / Data Structures
Hamdard University

if l <= A.heap-size and A[l] > A[i]
largest = l
else largest = i
if r <= A.heap-size and A[r] > A[largest]
largest = r
if largest != i
exchange A[i] with A[largest]
MAX-HEAPIFY(A,largest)

BUILD-MAX-HEAP(A)
A.heap-size = A.length
for i = [A.length/2] downto 1
MAX-HEAPIFY(A, i)
HEAPSORT(A)
BUILD-MAX-HEAP(A)
for i = A.length downto 2
exchange A[1] with A[i]
A.heap-size = A.heap-size - 1
MAX-HEAPIFY(A, 1)




Data Structures & Algorithms / Data Structures
Hamdard University


Heap Sort Implementation
#include<stdio.h>
#include<conio.h>
void manage(int *, int);
void heapsort(int *, int, int);
void main()
{
int arr[20];
int i,j,size,tmp,k;
clrscr();
printf("\n\t\t\t------- Heap sorting method -------\n\n");
printf("Enter the number of elements to sort : ");
scanf("%d",&size);
printf("Enter The Element In Array\n");
for(i=1; i<=size; i++)
{
scanf("%d",&arr[i]);
manage(arr,i);
}
j=size;
for(i=1; i<=j; i++)

Data Structures & Algorithms / Data Structures
Hamdard University

{
tmp=arr[1];
arr[1]=arr[size];
arr[size]=tmp;
size--;
heapsort(arr,1,size);
}
printf("\n\t\t\t------- Heap sorted elements -------\n\n");
size=j;
printf("Sorted Elements:\t");
for(i=1; i<=size; i++)
printf("%d\t ",arr[i]);
getch();
}
void manage(int *arr, int i)
{
int tmp;
tmp=arr[i];
while((i>1) && (arr[i/2]< tmp))
{
arr[i]=arr[i/2];
i=i/2;

Data Structures & Algorithms / Data Structures
Hamdard University

}
arr[i]=tmp;
}
void heapsort(int *arr, int i, int size)
{
int tmp,j;
tmp=arr[i];
j=i*2;
while(j<=size)
{
if((j < size) && (arr[j] < arr[j+1]))
j++;
if(arr[j] < arr[j/2])
break;
arr[j/2]=arr[j];
j=j*2;
}
arr[j/2]=tmp;
}




Data Structures & Algorithms / Data Structures
Hamdard University

Quick Sort
Quicksort, or partition-exchange sort, is a sorting algorithm developed by Tony Hoare that, on
average, makes O(n log n) comparisons to sort n items. In the worst case, it makes O(n2)
comparisons, though this behavior is rare. Quicksort is often faster in practice than other O(n log n)
algorithms. Additionally, quicksort's sequential and localized memory references work well with
a cache. Quicksort is a comparison sort and, in efficient implementations, is not a stable sort.
Quicksort can be implemented with an in-place partitioning algorithm, so the entire sort can be
done with only O(log n) additional space used by the stack during the recursion
Working of Quick Sort:
Quicksort is a divide and conquer algorithm. Quicksort first divides a large list into two smaller sub-
lists: the low elements and the high elements. Quicksort can then recursively sort the sub-lists.
The steps are:
1. Pick an element, called a pivot, from the list.
2. Reorder the list so that all elements with values less than the pivot come before the pivot, while
all elements with values greater than the pivot come after it (equal values can go either way).
After this partitioning, the pivot is in its final position. This is called the partition operation.
3. Recursively apply the above steps to the sub-list of elements with smaller values and separately
to the sub-list of elements with greater values.
The base case of the recursion is lists of size zero or one, which never need to be sorted.
Algorithm

Data Structures & Algorithms / Data Structures
Hamdard University

QUICKSORT(A, p, r)
1. if p < r
2. q = PARTITION(A, p, r)
3. QUICKSORT(A, p, q - 1)
4. QUICKSORT(A, q + 1, r)

PARTITION(A, p, r)
1. x = A[r]
2. i = p - 1
3. for j = p to r - 1
4. if A[j] <= x
5. i = i + 1
6. exchange A[i] with A[j]
7. exchange A[i + 1] with A[r]
8. return i + 1
Quick Sort Implementation
#include<stdio.h>
#include<conio.h>
void quicksort(int arr[], int lb, int ub);
void main()

Data Structures & Algorithms / Data Structures
Hamdard University

{
int arr[20], n, i;
clrscr();
printf("\n\t\t\t------Quick Sort------\n\n");
printf("Enter the size of the array:");
scanf("%d",&n);
printf("Enter the elements to be sorted:\n");
for(i=0;i < n;i++)
scanf("%d",&arr[i]);
quicksort(arr, 0, n-1);
printf("\n\t\t\t-----Quick Sorted Elements-----\n\n");
printf("Sorted array:");
for(i = 0; i < n; i++)
printf("\t%d ",arr[i]);
getch();
}
void quicksort(int arr[], int lb, int ub)
{
int pivot, i, j, temp;
if(lb < ub)
{
pivot = lb;

Data Structures & Algorithms / Data Structures
Hamdard University

i = lb;
j = ub;
while(i < j)
{
while(arr[i] <= arr[pivot] && i <= ub)
i++;
while(arr[j] > arr[pivot] && j >= lb)
j--;
if(i < j)
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
temp = arr[j];
arr[j] = arr[pivot];
arr[pivot] = temp;
quicksort(arr, lb, j-1);
quicksort(arr, j+1, ub);
}
}

Data Structures & Algorithms / Data Structures
Hamdard University

Merge Sort
In computer science, merge sort (also commonly spelled mergesort) is an O(n log n) comparison-
based sorting algorithm. Most implementations produce a stable sort, which means that the
implementation preserves the input order of equal elements in the sorted output. Mergesort is
a divide and conquer algorithm that was invented by John von Neumann in 1945. A detailed
description and analysis of bottom-up mergesort appeared in a report by Goldstine and Neumann
as early as 1948.
Working of Merge Sort
Conceptually, a merge sort works as follows:
1. Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is
considered sorted).
2. Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist
remaining. This will be the sorted list.
Algorithm
MERGE (A,p,q,r)
1. n1 = q + p + 1
2. n2 = r + q
3. let L[1. . n1 + 1] and R[1 . . n2 + 1] be new arrays
4. for i = 1 to n1
5. L[i] = A[p + i + 1]
6. for j = 1 to n2
7. R[j ] = A[q + j]
8. L[n1 + 1] = 9999

Data Structures & Algorithms / Data Structures
Hamdard University

9. R[n2 + 1] = 9999
10. i = 1
11. j = 1
12. for k = p to r
13. if L[i] <= R[j ]
14. A[k] = L[i]
15. i = i + 1
16. else A[k] = R[j]
17. j = j + 1
Merge Sort Implementation
#include<stdio.h>
#include<conio.h>
int arr[20];
void main()
{
int n,i;
clrscr();
printf("\n\t\t\t------Merge Sorting------\n\n");
printf("Enter the size of array\n");
scanf("%d",&n);
printf("Enter the elements:\n");
for(i=0; i < n; i++)
{
scanf("%d",&arr[i]);
}
merge_sort(arr,0,n-1);
printf("\n\n\t\t\t-----Merge Sorted Elements-----\n\n");
printf("Sorted array:\t");

Data Structures & Algorithms / Data Structures
Hamdard University

for(i=0; i < n; i++)
{
printf("\t%d",arr[i]);
}
getch();
}

int merge_sort(int arr[],int low,int high)
{
int mid;
if(low < high)
{
mid=(low+high)/2;
merge_sort(arr,low,mid);
merge_sort(arr,mid+1,high);
merge(arr,low,mid,high);
}
}
int merge(int arr[],int l,int m,int h)
{
int arr1[10],arr2[10];
int n1,n2,i,j,k;
n1=m-l+1;
n2=h-m;
for(i=0; i < n1; i++)
{
arr1[i]=arr[l+i];
}
for(j=0; j < n2; j++)
{
arr2[j]=arr[m+j+1];
}
arr1[i]=9999;
arr2[j]=9999;
i=0;
j=0;

Data Structures & Algorithms / Data Structures
Hamdard University

for(k=l; k <=h; k++)
{
if(arr1[i]<=arr2[j])
arr[k]=arr1[i++];
else
arr[k]=arr2[j++];
}
}
Lab Task
1. Create an array using C++ language that stores random 50 integers. Now write a function that reads that
array and stores the number in a linked list. Now perform the following tasks:
a. Write a function that sorts the linked list using heap sort and writes the sorted list to a new
array.
b. Write a function that sorts the linked list using quick sort and writes the sorted list to a new
array.
c. Write a function that sorts the linked list using merge sort and writes the sorted list to a new
array.

Você também pode gostar