Skip to content

Commit

Permalink
Merge pull request TheAlgorithms#16 from theycallmemac/patch-5
Browse files Browse the repository at this point in the history
Rename BubbleSort.c to Sorts/BubbleSort.c
  • Loading branch information
r0hit-gupta authored Aug 8, 2017
2 parents a18c71b + 9892a8e commit 47c45d0
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Sorts/BubbleSort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <stdio.h>
#include <stdlib.h>


int main(){

int* ARRAY=NULL;
int ContinueFilling=1; //This is to know if we should continue filling our array
int ARRAY_LENGTH=0,isSorted=0,i,TEMPORARY_ELEMENT;

//This code part is for filling our array
while(ContinueFilling){
printf("Enter the value number %d \n",ARRAY_LENGTH+1);
ARRAY=(int *)realloc(ARRAY,sizeof(int)*(ARRAY_LENGTH));
scanf("%d",&ARRAY[ARRAY_LENGTH]);
ARRAY_LENGTH+=1;
printf("would you enter an other value (1:Continue/0:Sort the actual array)?\n");
scanf("%d",&ContinueFilling);
}

//Then we sort it using Bubble Sort..

while(!isSorted){ //While our array's not sorted
isSorted=1; //we suppose that it's sorted
for(i=0;i<ARRAY_LENGTH-1;i++){ //then for each element of the array
if(ARRAY[i]>ARRAY[i+1]){ // if the two elements aren't sorted
isSorted=0; //it means that the array is not sorted
TEMPORARY_ELEMENT=ARRAY[i]; //and we switch these elements using TEMPORARY_ELEMENT
ARRAY[i]=ARRAY[i+1];
ARRAY[i+1]=TEMPORARY_ELEMENT;
}
}
}
//And we display it
for(i=0;i<ARRAY_LENGTH;i++){
printf("%d, ",ARRAY[i]);
}

return 0;
}

0 comments on commit 47c45d0

Please sign in to comment.