Hi! can someone help me to make this code littlest? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Hi! can someone help me to make this code littlest?

the next code (Array organizer) isnt that big, but im sure there is a way to make it littlest, can someone help me? https://code.sololearn.com/cUZPhJsw60sm/#cs namespace SoloLearn { class Program { static void Main(string[] args) { //this program order the numbers inside of an array and then prints it int[] arr = { 10, 1, 4, 6, 3, 5, 7, 2, 8, 9};//disordered numbers int min;//littlest number int may;//greatest number (dont ask why may...) int c = 0;//loop counter(i will use this variable for the exit mechanism) for (int x = 1; x < 9; x++)/*note that the array has 10 intergers but the loop starts with 1 and ends with 8, the following code will explain the why of this bounds (remember the zero index)*/ { if (arr[(x)-1]> arr[x] && arr[x] < arr[(x)+1]) { may = arr[(x) - 1]; min = arr[x]; // switching values position arr[x] = may; arr[(x) - 1] = min; } if (arr[(x)-1] < arr[x] && arr[x]>arr[(x)+1]) { may = arr[x]; min = arr[(x) + 1]; //switching values position arr[(x) + 1] = may; arr[x] = min; } if (arr[(x)-1]>arr[x] && arr[x] > arr[(x + 1)]) { min = arr[(x) + 1]; may = arr[(x) - 1]; //switching values position arr[(x) + 1] = may; arr[(x) - 1] = min; }/*the next lines of code insure that all the numbers are in the right position by running this loop the necessary times (Length of the array)*/ if (c == arr.Length)//loop exit

12th Apr 2017, 8:34 PM
Othienno Rafael Soto Esperança
Othienno Rafael Soto Esperança - avatar
3 Answers
12th Apr 2017, 8:54 PM
Giannis Tsirovasilis
Giannis Tsirovasilis - avatar
+ 1
this is the slowest in terms of runtime sorting code. but is the best for beginners.
12th Apr 2017, 9:06 PM
Giannis Tsirovasilis
Giannis Tsirovasilis - avatar
0
thanks you very much for your time, this is a real shorter way
12th Apr 2017, 9:05 PM
Othienno Rafael Soto Esperança
Othienno Rafael Soto Esperança - avatar