Skip to content

Instantly share code, notes, and snippets.

@HasanMukit
Created May 8, 2016 04:24
Show Gist options
  • Save HasanMukit/145279259fcdcc425411420ad32f5408 to your computer and use it in GitHub Desktop.
Save HasanMukit/145279259fcdcc425411420ad32f5408 to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<cs50.h>
int main ()
{
//promtig user for total item
printf("Number of items: ");
int totalItem=GetInt();
//promting user for input
printf("Give the integer items needs to be sorted\n");
// recording item list
int list[totalItem];
for (int count=0; count<totalItem; count++)
{
list[count]=GetInt();
}
// printing unsorted item list
for (int count=0; count<totalItem; count++)
{
printf("%d ",list[count]);
}
printf("\n");
int k=1; //
//sorting controll(pass) loop
for (int count=0; count<totalItem; count++)
{
bool condition=false; //loop continuation condition
//swapping loop
for(int j=0; j<totalItem-k; j++ )
{
//swapping
if(list[j]>list[j+1])
{
int temp=list[j];
list[j]=list[j+1];
list[j+1]=temp;
condition=true;
}
}
//printing element every sort
printf("list after %d iteration\n",k);
for (int count2=0; count2<totalItem; count2++)
{
printf("%d ",list[count2]);
}
printf("\n");
//early exit if sorting is done
if(condition==false || count==totalItem-2)
break;
k++; //track increment
}
// printing sorted item list
printf("Final sorted list: ");
for (int count=0; count<totalItem; count++)
{
printf("%d ",list[count]);
}
printf("\n");
printf("early exict at iteration %d\n",k);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment