Tuesday, June 2, 2020

sorting in c| c sorting | selection sorting | bubble sorting

Selection sorting in c
In selection sorting we compare first element of an array with other elements of  an array when first cycle completes smallest element comes at the top.
Then we compare second element of an array with rest of the elements at the end of second cycle second smallest element comes at the second position.
then we compare third element with rest of the elements and so on until all array sorted.

you tube link for selection sorting



Selection sorting in c++

//selection sorting in c language

#include<stdio.h>
#include<conio.h>
void  main()
{
int ar[5];
int i,j,temp;
for(i=0;i<5;i++)
{
printf("\n%dEnter value in an array:\n");
scanf("%d",&ar[i]);
}
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(ar[i]>ar[j])
{
temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
}
}
printf("sorted array is :\n");
for(i=0;i<5;i++)
{
printf("%d\n");
}
getch();
}


Bubble Sorting in C language

In  bubble sorting each of the element is compared to its adjacet element.

In this comparing if first element is greater than the second  the two elements are swapped.


//bubble sorting in c language
#include<stdio.h>
#include<conio.h>
void  main()
{
clrscr();
int ar[10];
int i,j,n,temp;
printf("Enter the size of array:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter value in an array:");
scanf("%d",&ar[i]);
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(ar[j]>ar[j+1])
{
temp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=temp;
}
}
}
printf("sorted array is :\n");
for(i=0;i<n;i++)
{
printf("%d");
}
getch();
}
bubble sort


 Insertion sort in c language


In this sorting first element is considerd as sorted
Each element in array is checked with previous elements
array divides each time in two parts sorted and unsorted
This sorting picks a element from unsorted list and keep it in sorted part

you tube link for insertion sort



insertion sort in c
Time and space complexity of sorting algorithm

Which sorting algorithm is best and why?
Quick sort algorithm is best among all sorting techniques because the time complexity of quick sort is o(n log n ) in best case o(n log n) in average case and o(n^2) in the worst case ans it has best in average case for most inputs

what is pivot in quick sort
a large array is partitioned into two arrays  one which hold value smaller than specified value say pivot and another array holds value greater than pivot value.

No comments:

Post a Comment

if you have any doubt. please let me know.

Data communication

 Communication means exchange of data between two or more Devices. Components of data communication - 1. Sender.    2. Receiver.     3. Mess...