0% found this document useful (0 votes)
10 views

Hello

The document discusses different sorting algorithms like bubble sort, selection sort, and their optimized versions through code examples. It contains code snippets to rotate an array, find unique elements in an array, implement basic and optimized bubble sort, selection sort and stable selection sort.

Uploaded by

Rakesh Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Hello

The document discusses different sorting algorithms like bubble sort, selection sort, and their optimized versions through code examples. It contains code snippets to rotate an array, find unique elements in an array, implement basic and optimized bubble sort, selection sort and stable selection sort.

Uploaded by

Rakesh Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

1.

Program to rotate array elements on left side by k times

#include<stdio.h>

int main()
{
int arr[100];
int i,j,temp,n,k;

printf("Enter the Size: ");


scanf("%d",&n);

printf("Enter the Elements: ");


for(i=0;i<n;i++)
scanf("%d",&arr[i]);

printf("Enter Number of Rotations: ");


scanf("%d",&k);

printf("\nBefore Rotations: \n");


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

//rotate array elements


for(i=0;i<k;i++)
{
temp=arr[0];
for(j=0;j<n-1;j++)
arr[j]=arr[j+k];

arr[j]=temp;
}

printf("\nAfter Rotations: \n");


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

return 0;
}

2. Program to print unique elements from array

#include<stdio.h>

int main()
{
int arr[100];
int i,j,k,n,status1,status2;

//i for current element


//j for previous elements
//k for next elements

printf("Enter Number of Elements: ");


scanf("%d",&n);

printf("Enter array Elements: ");


for(i=0;i<n;i++)
scanf("%d",&arr[i]);

for(i=0;i<n;i++) // currnet element


{
status1=0; // used for checking the cond inside loop is executed or not
for(j=0;j<i;j++)
{
if(arr[i]==arr[j])
{
status1=1;
break;
}
}
if(status1==0)
{
status2=0;
//search in next elements
for(k=i+1;k<n;k++)
{
if(arr[i]==arr[k])
{
status2=1;
break;
}
}
if(status2==0)
printf("%d\t",arr[i]);
}
}
return 0;
}

3. Basic bubble sort


#include<stdio.h>

void readArray(int b[],int n)


{
int i;

for(i=0;i<n;i++)
scanf("%d",&b[i]);
}

void printArray(int b[],int n)


{
int i;

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

void bubbleSort(int arr[],int n)


{
int i,j,temp;

for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}

int main()
{
int arr[50];
int n;

printf("Enter Size of array: ");


scanf("%d",&n);

printf("Enter Array Elements: ");


readArray(arr,n);

printf("\nBefore Sorting :\n");


printArray(arr,n);

bubbleSort(arr,n);

printf("\nAfter Sorting: \n");


printArray(arr,n);

return 0;
}

4. Optimized bubble sort


#include<stdio.h>

void readArray(int b[],int n)


{
int i;

for(i=0;i<n;i++)
scanf("%d",&b[i]);
}

void printArray(int b[],int n)


{
int i;

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

void optimisedBubbleSort(int arr[],int n)


{
int i,j,temp;
int status;

for(i=0;i<n-1;i++)
{
status=0;
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;

status=1;
}
}
if(status==0) // means swap is not occured entire phase
break;
}
}

int main()
{
int arr[50];
int n;

printf("Enter Size of array: ");


scanf("%d",&n);

printf("Enter Array Elements: ");


readArray(arr,n);

printf("\nBefore Sorting :\n");


printArray(arr,n);

optimisedBubbleSort(arr,n);

printf("\nAfter Sorting: \n");


printArray(arr,n);

return 0;
}
5. Selection sort

#include<stdio.h>

void readArray(int b[],int n)


{
int i;

for(i=0;i<n;i++)
scanf("%d",&b[i]);
}

void printArray(int b[],int n)


{
int i;

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

void selectionSort(int arr[],int n)


{
int i,j,min_indx,temp;
for(i=0;i<n-1;i++) // move the boundary one by one
{
//find the index of smallest element
min_indx=i;

for(j=i+1;j<n;j++)
{
if(arr[j]<arr[min_indx])
{
min_indx=j; // update the smallest element index
}
}
temp = arr[min_indx];
arr[min_indx]=arr[i];
arr[i]=temp;
}
}

6. Stable Selection Sort

#include<stdio.h>

void readArray(int b[],int n)


{
int i;
for(i=0;i<n;i++)
scanf("%d",&b[i]);
}

void printArray(int b[],int n)


{
int i;

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

void stableSelectionSort(int arr[],int n)


{
int i,j,min_indx,temp;

for(i=0;i<n-1;i++) // move the boundary one by one


{
//find the index of smallest element
min_indx=i;

for(j=i+1;j<n;j++)
{
if(arr[j]<arr[min_indx])
{
min_indx=j; // update the smallest element index
}
}
temp = arr[min_indx];

while(min_indx>i)
{
arr[min_indx]= arr[min_indx-1];
min_indx--;
}
arr[i]=temp;
}
}

int main()
{
int arr[50];
int n;

printf("Enter Size of array: ");


scanf("%d",&n);

printf("Enter Array Elements: ");


readArray(arr,n);

printf("\nBefore Sorting :\n");


printArray(arr,n);

stableSelectionSort(arr,n);

printf("\nAfter Sorting: \n");


printArray(arr,n);

return 0;
}

int main()
{
int arr[50];
int n;

printf("Enter Size of array: ");


scanf("%d",&n);

printf("Enter Array Elements: ");


readArray(arr,n);

printf("\nBefore Sorting :\n");


printArray(arr,n);

selectionSort(arr,n);
printf("\nAfter Sorting: \n");
printArray(arr,n);

return 0;
}

Tomorrow time : 2PM -5PM

“Have Patience and Behave Like Compiler”

You might also like