Insertion Sort (इंसर्शन सॉर्ट)

Insertion Sort (इंसर्शन सॉर्ट) :-

It is a simple and effective method of sorting in which the size of array is one more then the size of list. In this sorting method at the time of insertion an item is compared with all other existing elements of the list and larger elements are shifted by one place to make an empty space for input item then the item is stored at this specific place in the list.
Here we assume that lower limit of the data type of list is stored at index 0 like for int data type list[0]=-32768.     

यह सॉर्टिंग की एक सरल एवं प्रभावी युक्ति है इसमें ऐरे का साइज़, लिस्ट के साइज़ से एक अधिक लिया जाता है। इस सॉर्टिंग मेथड में एलिमेंट के इंसर्शन के समय उसे पूर्व में उपस्थित सभी एलेमेंट्स से जांचा जाता है एवं बड़े एलेमेंट्स को एक-एक स्थान शिफ्ट कर,एलिमेंट को निश्चित स्थान पर रखा जाता है। 
यह माना जाता है कि ऐरे के इंडेक्स 0 पर लिस्ट के डाटा टाइप की लोअर लिमिट को रखा गया है जैसे int डाटा टाइप के लिए list[0]= -32768 रखा गया है।   

Program/function/algorithm for Insertion Sort:- 

File Name:- isort.cpp

#include<iostream.h>
#include<conio.h>
#define N 6
void isort(){
int list[N],i,j,temp;
list[0]=-32768;
cout<<"Insertion Sort "<<endl<<"Enter Elements of List"<<endl;
for(i=1;i<N;i++){
cout<<"Enter "<<i<<" th element=";
cin>>list[i];
}
cout<<"Before Sorting List is:"<<endl;
for(i=1;i<N;i++){
cout<<list[i]<<" ";
}
for(i=1;i<N;i++){
temp=list[i];
j=i-1;
while(j>=0 && list[j]>temp){
list[j+1]=list[j];
j--;
}
list[j+1]=temp;
}
cout<<"After Sorting List is:"<<endl;
for(i=1;i<N;i++){
cout<<list[i]<<" ";
}
return;
}
void main(){
clrscr();
isort();
getch();
}


File Name:- isort.c

#include<stdio.h>
#include<conio.h>
#define N 6
void isort(){
int list[N],i,j,temp;
list[0]=-32768;
printf("Insertion Sort \nEnter Elements of List\n");
for(i=1;i<N;i++){
printf("Enter %d element=",i);
scanf("%d",&list[i]);
}
printf("Before Sorting List is:\n");
for(i=1;i<N;i++){
printf("%d ",list[i]);
}
for(i=1;i<N;i++){
temp=list[i];
j=i-1;
while(j>=0 && list[j]>temp){
list[j+1]=list[j];
j--;
}
list[j+1]=temp;
}
printf("After Sorting List is:\n");
for(i=1;i<N;i++){
printf("%d ",list[i]);
}
return;
}
void main(){
clrscr();
isort();
getch();
}

No comments:

Post a Comment

Stack Data Structure, Push, Pop and Peek Operations , Applications of Stack

Stack is a linear data structure. It is collection of ordered data elements. It is also known as LIFO system (last in first out). It means i...