0

want this code in C# ,

void sort(int *a, int size)
 {
   int i,j, swapped=0;
   for (j=size-1; j>0; j--) {
     swapped=0;
     for (i=0; i<j; i++) {
       if (a[i+1] < a[i]) {
          swap(a[i], a[i+1]); swapped=1
       }
     }
     if ( swapped == 0 ) break;
   }
 }
3
Contributors
2
Replies
4
Views
7 Years
Discussion Span
Last Post by bbman
0

Procedure signature void sort(int *a, int size) without any pointers is void sort(int[] a, int size) You can do the rest by yourself by copy/pasting the code to the editor which will tell you about syntax errors.

Procedure void swap(int a, int b) simply swaps the values of a and b. That's easy to write.

Bubble-sort algorithm works fine with a few items in an array. If you have large arrays (+100 items) consider using quick-sort algorithm instead.

.NET has also built-in Array.Sort() method which I would use myself.

0

In the rules, it says you must have at least attempted it yourself.
Nevertheless, you should add one to the swaps- it may be useful later.
You will need to make a swap event.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.