Advertisement
AvengersAssemble

ArraySort

Oct 31st, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1.             int nextVal = int.MaxValue, index = 0, n;
  2.             //Random rnd = new Random();
  3.             n = int.Parse(Console.ReadLine());
  4.             int[] arr = new int[n];
  5.             for (int i = 0; i < arr.Length; i++) //מעניק ערכים התחלתיים למערך
  6.             {
  7.                 arr[i] = int.Parse(Console.ReadLine());
  8.                 //arr[i] = rnd.Next(1, 100) ; //מאתחל מערך בערכים רנדומליים
  9.             }
  10.             for (int i = 0; i < arr.Length; i++) //ממיין מערך
  11.             {
  12.                 nextVal = int.MaxValue;
  13.                 for (int k = i; k < arr.Length; k++) //מחפש את האיבר הקטן ביותר שלא מוין
  14.                 {
  15.                     if (arr[k] < nextVal)
  16.                     {
  17.                         nextVal = arr[k];
  18.                         index = k;
  19.                     }
  20.                 }
  21.                 if (index != i) //מטרת התנאי היא לוודא שכל הערכים ימוינו ושלא ישוכתבו ערכים שעוד לא מוינו
  22.                         arr[index] = arr[i];
  23.                 arr[i] = nextVal;
  24.             }
  25.             foreach (int currentInt in arr) //מדפיס מערך
  26.                 Console.Write(currentInt + " ");
  27.             Console.WriteLine();
  28.             /* for(int i = 0; i < arr.Length; i++)
  29.              * Console.Write(currentInt + " ");
  30.              * Console.WriteLine(); */ //הדפסת מערך בעזרת לולאת for
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement