#include "stdio.h"
#include "conio.h"
//swapping function
void swap(int* a, int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
//sorting function
void sort(int* a){
int i = 0;
int j = 0;
int temp = 0;
int flag = 1;
int m =0;
//optimized bubble sort using flag Variable which stops the loop after the array is sorted
for (i = 0; ((i < 5) && (flag == 1)); i++ ){
flag = 0;
m = i;
for (j = 0; j < 5-i; j++){
if(a[j] > a[j+1]){
swap(&a[j],&a[j+1]);
flag = 1;
}
}
}
}
//main function
int main(){
int* array;
int i = 0;
//Array creation dynamically
array = (int*) malloc (5*sizeof(int));
//Sample array
array[0] = 78;
array[1] = 56;
array[2] = 93;
array[3] = 19;
array[4] = 104;
for( i = 0; i < 5; i++){
printf("%d \t", array[i]);
}
printf("\n\nAfter Sorting \n\n");
sort(array);
for( i = 0; i < 5; i++){
printf("%d \t", array[i]);
}
getch();
return 0;
}
#include "conio.h"
//swapping function
void swap(int* a, int *b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}
//sorting function
void sort(int* a){
int i = 0;
int j = 0;
int temp = 0;
int flag = 1;
int m =0;
//optimized bubble sort using flag Variable which stops the loop after the array is sorted
for (i = 0; ((i < 5) && (flag == 1)); i++ ){
flag = 0;
m = i;
for (j = 0; j < 5-i; j++){
if(a[j] > a[j+1]){
swap(&a[j],&a[j+1]);
flag = 1;
}
}
}
}
//main function
int main(){
int* array;
int i = 0;
//Array creation dynamically
array = (int*) malloc (5*sizeof(int));
//Sample array
array[0] = 78;
array[1] = 56;
array[2] = 93;
array[3] = 19;
array[4] = 104;
for( i = 0; i < 5; i++){
printf("%d \t", array[i]);
}
printf("\n\nAfter Sorting \n\n");
sort(array);
for( i = 0; i < 5; i++){
printf("%d \t", array[i]);
}
getch();
return 0;
}