Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
2 views9 pages

12 - Data Structures - Sorting

The document discusses internal and external sorting algorithms, highlighting that internal sorts occur in main memory and include methods like Bubble Sort and Quick Sort, while external sorts handle larger datasets using slower external memory. It details the Quick Sort algorithm and its steps, as well as the Merge Sort algorithm, which uses a divide-and-conquer approach to sort data efficiently. The analysis of both sorting methods indicates their time complexities, with Quick Sort averaging O(n log n) and Merge Sort also achieving O(n log n) but requiring additional space.

Uploaded by

Kamal joshi
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)
2 views9 pages

12 - Data Structures - Sorting

The document discusses internal and external sorting algorithms, highlighting that internal sorts occur in main memory and include methods like Bubble Sort and Quick Sort, while external sorts handle larger datasets using slower external memory. It details the Quick Sort algorithm and its steps, as well as the Merge Sort algorithm, which uses a divide-and-conquer approach to sort data efficiently. The analysis of both sorting methods indicates their time complexities, with Quick Sort averaging O(n log n) and Merge Sort also achieving O(n log n) but requiring additional space.

Uploaded by

Kamal joshi
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/ 9

Internal Sort

An internal sort is any data sorting process that takes place entirely within the main memory
(RAM) of a computer. This is possible whenever the data to be sorted is small enough to all be
stored in the main memory.
Internal Sorting algorithms are
1. Bubble Sort
2. Insertion Sort
3. Quick Sort
4. Heap Sort
5. Merge Sort
6. Radix Sort
7. Selection sort

Suppose data set is small enough to be held by the main memory and bubble sort is applied to
sort the records of a data set. In a Bubble sort, adjacent records are swapped in order to get
them into the right order, so that records appear to ‘bubble’ up and down through the data
space. If all the swapping is done inside main memory, then bubble sort works fine. But, if
there is a large data set to be sorted using bubble sort, then this has to be done in by breaking
the data set into chunks. When we have sorted all the records in first chunk, we move on to the
second chunk, but we find that some of the records in the first chunk need to ‘bubble through’
the second chunk, and vice versa i.e., there are records in the first chunk that belong to the
second chunk, and records in the second chunk that belong to the first chunk or to the other
chunks. This will cause the chunks to be read and written back to disk many times, resulting in
a considerable degradation of performance. If the data can all be held in memory as one large
chunk, then this performance degradation is avoided.

If the data set to be sorted is so large that it is not possible to store entire data in a memory,
then a chunk of data is taken into a memory. The remaining data is normally held on some
larger, but slower medium, like a hard-disk. Any reading or writing of data to and from this
slower media can slow the process considerably.
Some algorithms handle external sorting in a better way. A Merge sort algorithm breaks the
data up into chunks, sorts the chunks by some other algorithm (bubble sort or quick sort or any
other sorting algorithm) and then recombines the chunks so that each recombined chunk is in
the desired order. This approach minimizes the number of read and write operations of data-
chunks from a disk and is appears to be an efficient external sorting method.

External Sort
An external sorting is a term for a class of sorting algorithms that can handle massive amounts
of data. External sorting is required when the data being sorted do not fit into the main memory
usually RAM and therefore must reside in the slower external memory usually a secondary
storage device as a hard disk. External sorting typically uses a hybrid sort-merge strategy. There
are two phases: the sorting phase and the merging phase. In the sorting phase, chunks of data
small enough to fit in main memory are read, sorted, and written out to a temporary file. In the
merge phase, the sorted sub-files are combined into a single larger file.
For Example:
One example of external sorting is the external merge sort algorithm, which sorts chunks that
each fit in RAM, then merges the sorted chunks together.
For example, for sorting 400 MB of data using only 100 MB of RAM:
1. Read 100 MB of the data in main memory and sort by any internal sorting algorithm
such as bubble sort or quick sort.
2. Write the sorted data into hard disk.
3. Repeat steps 1 and 2 until all of the 100 MB chunks (there are 400MB / 100MB = 4
chunks) are sorted, which now need to be merged into one single output file.
4. Read the first 20 MB (= 100MB / (4 chunks + 1 output buffer)) of each sorted chunk
into input buffers in main memory and allocate the remaining 20 MB for an output
buffer. Perform a 4-way merge and store the result in the output buffer. Whenever the
output buffer fills, write it to the final sorted file and empty it. Whenever any of the 4
input buffers get empty, fill it with the next 20 MB of its associated 100 MB sorted
chunk until no more data from the chunk is available. This is the key step that makes
external merge sort work externally as each chunk does not have to be loaded
completely, but sequential parts of the chunk can be loaded as needed.

Quick Sort
Algorithm: Quick Sort
1. If n < = 1, then return.
2. Pick the first element of an array as pivot.
3. Place pivot at the proper place in an array.
4. Create two sub-lists left and right side of a pivot element.
5. Repeat steps 2, 3, 4 until all the elements are placed at their proper place.

For placing the pivot element at the proper place:


1. Compare the pivot element one by one from right to left for getting the element having value
less than the pivot element.
2. Swap pivot element with that element.
3. Compare the pivot element from the swapped element’s position from left to right for getting
the element having higher value than the pivot node.
4. Swap the pivot element with that element.
5. Repeat steps 1,2,3,4 until the pivot element is placed at the proper position.

Example:

Let us take the following unsorted list 30, 40, 10, 5, 15, 45, 35, 37.
Choose 30 as a pivot element.
30 40 10 5 15 45 35 37
Compare pivot 30 from right to left. 30 >15, interchange them.

15 40 10 5 30 45 35 37
Compare 30 from left to right. 30 <40, interchange them.

15 30 10 5 40 45 35 37
Compare 30 from right to left. 30 > 5, interchange them.

15 5 10 30 40 45 35 37
Compare 30 from left to right starting from 5. As 30>10, there is no interchange. Now,
Element 30 is placed at proper position. Stop the process. Create two sub-lists as

15 5 10 30 40 45 35 37

Sublist 1 Sublist 2

Same process is repeated to get all elements at right places.

Sublist 1:
Choose 15 as a pivot element.
15 5 10

Compare pivot 15 from right to left. 15 >10, interchange them.


10 5 15

Compare 15 from left to right starting from 5. As 15>5, there is no interchange. Pivot 15 is
placed at the proper position. Stop the process. Divide the list
10 5 15

Sublist 3

Sublist 3:
Choose 10 as a pivot element.
10 5

Compare pivot 10 from right to left. 10 >5, interchange them.


5 10

Pivot 10 is placed at the proper position. Divide the list, but now there are lists with the single
element. Element 5 is placed at the proper position. Stop the process.
Sublist 2:
Take 40 as a pivot element.
40 45 35 37

Compare pivot 40 from right to left. 40 >37, interchange them.


37 45 35 40

Compare 40 from right to left starting from 45. 45 > 40, interchange them.

37 40 35 45

Compare pivot 40 from right to left from 35. 40 >35, interchange them.
37 35 40 45

The pivot 40 is placed at the proper position. Stop the process. Divide the list.

37 35 40 45

Sublist 4 Sublist 5

Sublist 4:
Choose 37 as a pivot node.
37 35
Compare pivot 37 from right to left from 35. 37 >35, interchange them.

35 37

The pivot 37 is place at the proper position. Divide the list, but each list has a single element,
therefore, they are at proper position. Stop the process.

The list is sorted as all the elements are placed at their proper positions.

5 10 15 30 35 37 40 45

/*Program of Quick Sort*/


#include<stdio.h>
#include<stdlib.h>
#define MAX 10

void quicksort(int *, int, int);


int main()
{
int arr[MAX], size, i;
printf("Enter size of the array: ");
scanf("%d", &size);
printf("Enter %d elements: ",size);
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

quicksort(arr, 0, size-1);

printf("Sorted elements: ");

for(i=0; i<size; i++)


{
printf(" %d", arr[i]);
}
return 0;
}

void quicksort(int *a, int first, int last)


{
int pivot, j, temp, i;
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);

}
}

Analysis of Quick Sort


The best case occurs when the partition process always picks the middle element as pivot.
The best case complexity of a quick sort is O(n log n).
The average case complexity is also O(n log n).

Merge Sort
The rule of Divide and Conquer approach is followed by the merge sort algorithm. In a merge
sort algorithm, the unsorted list with N elements is divided into N sublists, each having one
element. A list consisting of one element is always sorted. Then, it repeatedly merges these
sublists, to generate new sorted sublists, and in the end, only one sorted list is generated.

Let us take an array having the elements as given below:


15 10 5 20 25 13 12 9 7 18

Pass 1: Initially an array is divided and all the elements are taken as an individually entity and
therefore all are sorted. Now, merge and sort the elements in a pair of two.
10 15 5 20 13 25 9 12 7 18

Pass 2: Merge and sort the sorted pairs of size two into the pair of size four
5 10 15 20 9 12 13 25 7 18

Pass 3: Merge and sort the sorted pairs into a sorted pair
5 9 10 12 13 15 20 25 7 18
Pass 4: Merge the two sorted pairs into one
5 7 9 10 12 13 15 18 20 25
The merge sort divides the given unsorted array into two halves, calls itself for the two halves
and then merges the two sorted halves. The merge() function is used here for merging two
halves. The function merge(arr, l, m, n) is the main process which assumes that arr[1, . . . , m]
and arr[m+1, . . ., n] are sorted and then merges the two sorted sub-arrays into one.

Algorithm: mergeSort [arr, 1, n]


1. If (n > l)
i. Find the middle point m to divide the array into two halves as
m = (l+n)/2
ii. Call mergeSort for first half as
mergeSort(arr, l, m)
iii. Call mergeSort for second half as
mergeSort(arr, m+1, n)
iv. Merge the two halves sorted in step 2 and 3 as
merge(arr, l, m, n)
[End of if statement]
2. End

/*Program for Merge Sort an unsorted array recursively*/


#include<stdlib.h>
#include<stdio.h>

void merge(int *, int, int, int);


void mergeSort(int *, int, int );
void display (int *, int);

int main()
{
int arr[] = {12, 11, 13, 5, 6, 7};
int size = sizeof(arr)/sizeof(arr[0]);

printf("Given array is \n");


display(arr, size);

mergeSort(arr, 0, size - 1);

printf("\nSorted array is \n");


display(arr, size);
return 0;
}
void merge(int *a, int l, int m, int n)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = n - m;

int L[n1], R[n2]; /* create temp arrays */

for (i = 0; i < n1; i++) /* Copy data to temp arrays L[] and R[] */
L[i] = a[l + i];
for (j = 0; j < n2; j++)
R[j] = a[m + 1+ j];

/* Merge the temp arrays back into arr[l..n]*/


i = 0; /* Initial index of first subarray*/
j = 0; /* Initial index of second subarray*/
k = l; /* Initial index of merged subarray*/
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
a[k] = L[i];
i++;
}
else
{
a[k] = R[j];
j++;
}
k++;
}

/* Copy the remaining elements of L[], if it is not exhausted */


while (i < n1)
{
a[k] = L[i];
i++;
k++;
}

/* Copy the remaining elements of R[], if it is not exhausted */


while (j < n2)
{
a[k] = R[j];
j++;
k++;
}
}

void mergeSort(int *a, int l, int n)


{
int m;
if (l < n)
{
m = (l+n)/2;

mergeSort(a, l, m); /*Sort first half*/


mergeSort(a, m+1, n); /*sort second half*/

merge(arr, l, m, n);
}
}

void display(int *a, int size)


{
int i;
for (i=0; i < size; i++)
printf("%d ", a[i]);
printf("\n");
}

Analysis of Merge Sort


Suppose an array of size n is to be sorted using this method. Here, we take elements in pair and
merge with another pair after sorting, therefore it requires at the most log2n passes. In each
pass, total number of comparisons can be at the most n. Therefore, merge sort requires n x log2
n comparisons. Thus, merge Sort is very fast and it has a time complexity of O (n log n). It is
also a stable sort, which means the ‘equal’ elements are ordered in the same order in the sorted
list. The main disadvantage of merge sort is that it requires an extra space of O(n).

You might also like