bubble sort

Hello I have a question where do I have to put printf and what should I print in buble sort. I have an int array and would like to order it asc.

1
2
3
4
5
6
7
8
9
10
11
int nal[250];
int b, c, temp;
  for (b = (4 - 1); b > 0; b--){
     for (c = 1; c <= b; c++){
        if (nal[c-1] > nal[c]){
           temp = nal[c-1];
           nal[c-1] = nal[c];
           nal[c] = temp;
        }
     }
}

You should print the unsorted array, then sort it and then print it again, so something like this:
Unsorted:
2 7 4 6 1

Sorted:
7 6 4 2 1
Last edited on
So I put print after all this code? Like
1
2
3
for(int i = 0; i < 4; i++){
printf("%d",nal[i])
}
Last edited on
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
using namespace std;
int main()
{
    int arr[]={12,4,56,89,64};
    int i, j, n=5;   //size of array = 5
    for(i=0; i<n; i++)
    {
           for(j=0; j<n-1; j++)
           {
                  if(arr[j]>arr[j+1])
                  {
                         int temp;
                         temp=arr[j];
                         arr[j]=arr[j+1];
                         arr[j+1]=temp;
                  }
           }
    }
    for(i=0; i<n; i++)
      cout<<arr[i]<<" ";  
    return 0;
}


The output will be:

4 12 56 64 89

Topic archived. No new replies allowed.