#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void bubbleSort(int *array,int n){
int i,j;
for( i=0; i<n; i++) //loop through all of element in the arraj
{
for( j=0; j<n-1; j++)//from the bigining of the arraj to the last element
{
if(array[j]>array[j+1])//if the arraj of the left element > arraj of the adjason element
{
int temp = array[j+1]; //create a temp element = the adj-son element
array[j+1] = array[j];// move the adj-son element to the left element
array[j] = temp; // move the left to the adj-son
}
}
}
}
int main(){
int arr[]= {1,4,5,6,9,20,49,7};
int n= sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr,n);
int i;
for(i=0; i < 8; i++){
printf("%d ",arr[i]);
}
return 0;
}
No comments:
Post a Comment