From 382f08483417ef8d1cbc6e23e3654d4a58daddb7 Mon Sep 17 00:00:00 2001 From: Mehdi ALAOUI Date: Tue, 4 Apr 2017 03:29:30 +0100 Subject: [PATCH] Optimizing the algorithm, and improving the array filling --- BubbleSort.c | 75 +++++++++++++++++++++++++++------------------------- 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/BubbleSort.c b/BubbleSort.c index c5b546ce..15827f07 100644 --- a/BubbleSort.c +++ b/BubbleSort.c @@ -1,37 +1,40 @@ -/* ALGORITHM -1. Start from left hand side -2. Compare first two numbers -3. If first_number > second_number than swap both number position. And if first_number < second_number than these compare next two numbers i.e. second_number and third_number. -4. Step-3 process repeat until there are no more numbers left to compared. -*/ +#include +#include -#include -#include -#include -int main() -{ - int i,j,temp,SIZE; - printf("Enter number of elements to be sorted : "); - scanf("%d",&SIZE); - int *arr = (int*)malloc(SIZE*sizeof(int)); - for(i=0; iARRAY[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