Answer to Question #3276 in C++ for Jakir

Question #3276
I want to get some examples of Bubble short program of C++
1
Expert's answer
2011-07-01T11:13:02-0400
#include //bubble sort
void bubbleSort(int mas[], const int n){
for(int i = 0; i < n; i++){
for(int o = 0; o < n-1-i; o++){
if(mas[o] > mas[o+1]){
//swap elements
int x = mas[o];
mas[o] = mas[o+1];
mas[o+1] = x;
}
}
}
}

int main(){
int n;
printf("Input quantity: ");
scanf("%d",&n);
if(n<=0){
return 0;
}
int* mas = new int[n];
//Enter numbers
printf("Enter numbers
");
for(int i = 0; i < n; i++){
scanf("%d",&mas[i]);
}
//sort numbers
bubbleSort(mas,n);
//print sorted array
printf("Result:
");
for(int i = 0; i < n; i++){
printf("%d ",mas[i]);
}

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS