Bubblesort

Pages: 12
@Chervil could you say me where the problem is?
Last edited on
If you want to print the progress, then insert print after every swap.
Last edited on
i did it in the funktion sor
void sort::sor()
{
for(j=0;j<10;j++)
for(i=j+1;i<10;i++)
{if(a[j]>a[i]){
swap ();
print();}
}}
but it is giving me in the output:
3 9 5 6 1 7 2 0 8 4
3 9 5 6 1 7 2 0 8 4
1
2
3
4
5
6
 void print(data (&arr)[size])
   {
        for(int i=0; i<size;++i)
        std::cout<<arr[i]<<' ';
        std::cout<<'\n';
   };


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 void operator()(data (&arr)[size])
   {
       bool is_sorted=false;
       
       while(!is_sorted)
       { 
        is_sorted=true;
        for(int unsigned i=0; i<size-1;++i)
        {
        if(arr[i]>arr[i+1]) 
          {
            swap(arr[i],arr[i+1]);
            print(arr);
            is_sorted=false;
          }
        }
      }
   };


Output:
1 5 2 9 8 0 
1 5 2 8 9 0 
1 5 2 8 0 9 
1 2 5 8 0 9 
1 2 5 0 8 9 
1 2 0 5 8 9 
1 0 2 5 8 9 
0 1 2 5 8 9 
0 1 2 5 8 9 
Last edited on
i have to do with my code because my university wants the program with 3 funktions which i wrote it :)
but im not sure if i have to do with sort::sort
Topic archived. No new replies allowed.
Pages: 12