Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- static void Main()
- {
- Console.WriteLine("Sorting numbers in ascending order.");
- // int[] unsorted = {7, 2, 5, 4, 9, 1, 6, 3, 8};
- // int[] unsorted = {9, 8, 7, 6, 5, 4, 3, 2, 1};
- int[] unsorted = {4, 3, 2, 1};
- int tmp = 0, pass = 0, swaps = 0;
- int unsortedNumLen = unsorted.Length;
- DisplayResults("\nNumbers unsorted.", unsorted);
- //sort
- while(pass != unsortedNumLen)
- {
- for(int i=0,j=1; i < unsortedNumLen-1 && j < unsortedNumLen; i++,j++)
- {
- if (unsorted[i] > unsorted[j])
- {
- pass = 0;
- swaps++;
- Console.Write("Swapping {0} and {1}:\t", unsorted[i], unsorted[j]);
- tmp = unsorted[i];
- unsorted[i] = unsorted[j];
- unsorted[j] = tmp;
- printArray(unsorted);
- }
- else pass++;
- }
- }
- DisplayResults("\nNumbers sorted.", unsorted);
- Console.WriteLine("Number of swaps: {0}\n",swaps);
- }
- static void printArray(int[] numbers)
- {
- foreach(int num in numbers)
- Console.Write("{0} ", num);
- Console.WriteLine();
- }
- static void CurrentTimeStamp()
- {
- Console.Write(" ({0})\n",DateTime.Now);
- }
- static void DisplayResults(string msg, int[] unsorted)
- {
- Console.Write(msg);
- CurrentTimeStamp();
- Console.WriteLine();
- printArray(unsorted);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement