Advertisement
AvengersAssemble

Array.Sort()

Jan 2nd, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.73 KB | None | 0 0
  1.             Random rnd = new Random();
  2.             int tmp, maxIndex = 0;
  3.             int[] arr = new int[20];
  4.             for (int i = 0; i < arr.Length; i++)
  5.                 arr[i] = rnd.Next(0, 21);
  6.             for (int i = 0; i < arr.Length; i++)
  7.             {
  8.                 for (int k = i; k < arr.Length; k++)
  9.                 {
  10.                     if (arr[k] > arr[maxIndex]) //change to if(arr[k] < arr[maxIndex]) to sort from small to big
  11.                         maxIndex = k;
  12.                 }
  13.                 tmp = arr[i];
  14.                 arr[i] = arr[maxIndex];
  15.                 arr[maxIndex] = tmp;
  16.             }
  17.  
  18.             //result example: 10, 9, 9, 7, 4, 3
  19.             //result small to big: 3, 4, 7, 9, 9, 10
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement