Beruflich Dokumente
Kultur Dokumente
UNIT-4
Introduction to sorting:
Sorting can be referred as a process of arranging the elements in an ordered manner. Sorting arranges data in a
sequence which makes searching easier.
Sorting is one of the methods in the concept of data structure which can be used to deal with the data in a good
and efficient manner.
Every record which is going to be sorted will contain one key. Based on the key the record will be sorted. For
example, suppose we have a record of students, every such record will have the following data:
o Roll No.
o Name
o Age
o Class
Here Student roll no. can be taken as key for sorting the records in ascending or descending order. Now suppose
we have to search a Student with roll no. 15, we don't need to search the complete record we will simply search between
the Students with roll no. 10 to 20.
Sorting classification:
The concept of sorting can be classified into two ways they are ascending order and descending order.
Ascending order means arranging of the elements from smallest value to largest value.
Example:
1 2 5 7 21 25 (Ascending order)
25 21 7 5 2 1 (Descending order)
Descending order means arranging of the elements from largest value to smallest value.
Sorting techniques:
There are many types of sorting techniques, in which each one will follow different procedures.
Following are some sorting techniques which we will be covering in next sections.
1. Bubble Sort
2. Insertion Sort(straight insertion sort, list insertion sort, binary insertion sort)
3. Selection Sort
4. Quick Sort
5. Merge Sort
6. Heap Sort
7. Shell sort
8. External sort
Bubble sort:
Definition:
Program:
#include<stdio.h>
#include<conio.h>
int main( )
{
int a[100];
int i, j, temp, n ;
printf("how many numbers you want to sort : \n");
scanf("%d",&n);
printf("Enter %d number values you want to sort\n", n);
for(j=0; j<n; j++)
scanf("%d",&a[j]);
for(j=1;j<n;j++)
{
for(i=0; i<n; i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
Output:
selection sort:
Definition:
Selection Sort algorithm is used to arrange a list of elements in a particular order (Ascending or
Descending).
Selection sorting is conceptually the simplest sorting algorithm.
This algorithm first finds the smallest element in the array and exchanges it with the element in the first position.
Then find the second smallest element and exchange it with the element in the second position, and continues in
this way until the entire array is sorted.
In selection sort, the first element in the list is selected and it is compared repeatedly with
remaining all the elements in the list.
Note:
The above Selection sort, described uses swapping of data which makes the algorithm Unstable,
while Selection sort can also be implemented as Stable sort, by selecting the smallest element and putting
the element before the first element of the list i.e. instead of swapping the first element with the smallest
element, we are sweeping the first element to second position, second to third and so on.
(Or)
Selecting Second smallest from the list and comparing with the second position
Selecting the third element and comparing with the remaining element and swapping
Already sorted
Code:
#include <stdio.h>
void main()
{
//declaring variables
int array[1000],n,i;
Insertion sort:
The procedure compare first and second values and put them in order or correct position by
inserting, now take out third one and compare it with second and first and put it in right position.
Now take out fourth and so on. This approach of inserting element in a sorted list is called
as Insertion sort.
C program for Insertion Sort. Insertion sort is one of the simple-stable-in places Sorting
Algorithm which is efficient when applied over small set of data.
While insertion sort takes more time to perform sorting over large list i.e less efficient for large
set of data, as compare to other sorting algorithm.
Step 1: Assume that first element in the list is in sorted portion of the list and remaining all elements are
in unsorted portion.
Step 2: Consider first element from the unsorted list and insert that element into the sorted list in order
specified.
Step 3: Repeat the above process until all the elements from the unsorted list are moved into the sorted
list.
Example
void main(){
getch();
}
Output:
5 6 3 1 2 4
1 2 3 4 5 6
Source Code:
#include<stdio.h>
struct node
{
int data;
struct node *next;
};
if(temp==NULL)
{
//Executes when linked list is empty
ptr->next=NULL;
head=ptr;
return;
}
if(data<temp->data)
{
//Executes if given data is less than data in first node of linked list
ptr->next=head;
head=ptr;
return;
}
else
{
while(temp!=NULL)
{
if(data>temp->data)
{
//Traverse to location we want to insert the node + 1 node
prev=temp;
temp=temp->next;
continue;
}
else
{
//Insert the node
prev->next=ptr;
ptr->next=temp;
void print()
{
struct node *temp=head;
printf("\nList:");
while(temp!=NULL)
{
printf("\n%d ",temp->data);
temp=temp->next;
}
}
int main()
{
insert_sort(23);
print();
insert_sort(10);
print();
insert_sort(15);
print();
insert_sort(45);
print();
insert_sort(50);
print();
return 0;
}
Output:
List:
23
List:
10 23
List:
10 15 23
List:
10 15 23 45
List:
10 15 23 45 50
Quick sort:
Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into smaller arrays.
A large array is partitioned into two arrays one of which holds values smaller than the specified value, say pivot, based
on which the partition is made and another array holds values greater than the pivot value.
It comes under divide and conquer algorithm which divides the large set of array in small by picking up a pivot
element as a reference element, and do sorting.
#include <stdio.h>
void quick_sort(int[],int,int);
int partition(int[],int,int);
int main()
{
int a[50],n,i;
printf("How many elements?");
scanf("%d",&n);
printf("\nEnter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick_sort(a,0,n-1);
printf("\nArray after sorting:");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
do
{
do
i++;
while(a[i]<v&&i<=u);
do
j--;
while(v<a[j]);
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}while(i<j);
a[l]=a[j];
a[j]=v;
return (j);
}
Output:
Merge sort:
An example of merge sort in C is given below. First divide the list into the smallest unit (1 element), then
compare each element with the adjacent list to sort and merge the two adjacent lists. Finally all the elements are
sorted and merged.
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
return 0;
}
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid); //left recursion
mergesort(a,mid+1,j); //right recursion
merge(a,i,mid,mid+1,j); //merging of two sorted sub-arrays
}
}
Output:
Shell sort:
Shell sorting was first introduced by Donald Shell.
It generalized as exchanging sort, such as bubble or insertion sorting, by allowing the comparison and exchange of
elements that lie far apart.
The running time of Shell sort is heavily dependent on the gap sequence it uses.
For many practical variants, determining their time complexity remains an open problem.
step.1:
Set up a inc number. inc number is set according to given elements in the list.
step.2:
mark each elements which is comes in inc element.
For example, if list contains 10 elements then and we assume inc is 3 then, marking of elements such as next marking element,
add last element +3 to get next element and so on. Then marking element would be 1st element, 4th element(add 3), 7th
element(add 3), 10th element.
89 46 99 12 33 14 69 41 33 28
1 2 3 4 5 6 7 8 9 10 [index number]
step.3:
sort marking elements such as smallest to greater is set as left to right and not change remain element.
For example, we apply this step in above example:
12 46 99 28 33 14 69 41 33 89
step.4:
reduce inc number to one i.e. if inc number is earlier is 3 then now it would be 3-1 = 2.
step.5:
Repeat step 2,3 and 4 till all the elements not sorted.
35 12 14 9 15 45 32 95 40 5
//assume inc=3
//Now marking 1st element, 1+3=4th element, //4+3=7th element, 7+3=10th element
35 12 14 9 15 45 32 95 40 5
//step-3 i.e. sorting of marked elements
5 12 14 9 15 45 32 95 40 35
//now sorting of marked elements
5 12 14 9 15 45 32 95 40 35
//Now inc=2-1=1
//Now every elements all marked because inc=1
5 12 14 9 15 45 32 95 40 35
//sorting of marked elements
5 9 12 14 15 32 35 40 45 95
int main()
{
int i, n, a[10];
printf("Enter the number of elements :: ");
scanf("%d",&n);
printf("Enter the elements :: ");
for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
ShellSort(a,n);
printf("The sorted elements are :: ");
for(i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
OUTPUT:
Enter the number of elements: 6
Enter the elements: 50 30 10 40 20 60
The sorted elements are: 10 20 30 40 50 60
Unsorted elements
45 33 3 17 25 80
80
33 45
17 25
3
Program for heap tree:
#include<stdio.h>
void heapsort(int[],int);
void heapify(int[],int);
void adjust(int[],int);
main() {
int n,i,a[50];
system("clear");
printf("\nEnter the limit:");
scanf("%d",&n);
printf("\nEnter the elements:");
Output:
Enter the limit:
5
Enter the list
20 10 50 40 30
Sorted list is
10 20 30 40 50.
External sort:
External sorting is 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 of a computing device (usually RAM)
and instead they must reside in the slower external memory, usually a hard disk drive.
External sorting typically uses a hybrid sort-merge strategy. 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 subfiles are combined into a single
larger file.
External merge sort is based on the internal sorting and external merging principle.
1. Divide the data set into run lists that fit into main memory(RAM)
2. Sort this run lists with internal sorting
3. Merge this runs and build the completely sorted data set.
Run 1
Run 1 and 2
Run 2
Run 1 and 2, 3, 4
Run 3
Run 3 and 4
Run 4
Example: