Advertisement
ivandrofly

Sorting

Mar 5th, 2015
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.87 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApplication2
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int[] arr = { 800, 11, 50, 771, 649, 770, 240, 9 };
  10.             InsertionSort(arr);
  11.             int temp = 0;
  12.  
  13.             for (int write = 0; write < arr.Length; write++)
  14.             {
  15.                 for (int sort = 0; sort < arr.Length - 1; sort++)
  16.                 {
  17.                     if (arr[sort] > arr[sort + 1])
  18.                     {
  19.                         temp = arr[sort + 1];
  20.                         arr[sort + 1] = arr[sort];
  21.                         arr[sort] = temp;
  22.                     }
  23.                 }
  24.             }
  25.  
  26.             for (int i = 0; i < arr.Length; i++)
  27.                 Console.Write(arr[i] + " ");
  28.  
  29.             Console.ReadKey();
  30.         }
  31.  
  32.  
  33.         public static void InsertionSort(int[] intArray)
  34.         {
  35.             Console.WriteLine("==========Integer Array Input===============");
  36.             for (int i = 0; i < intArray.Length; i++)
  37.             {
  38.                 Console.WriteLine(intArray[i]);
  39.             }
  40.  
  41.             int temp, j;
  42.             for (int i = 1; i < intArray.Length; i++)
  43.             {
  44.                 temp = intArray[i];
  45.                 j = i - 1; // 0
  46.  
  47.                 //{ 800, 11, 50, 771, 649, 770, 240, 9 };
  48.                 while (j >= 0 && intArray[j] > temp)
  49.                 {
  50.                     intArray[j + 1] = intArray[j]; // 11
  51.                     j--;
  52.                 }
  53.  
  54.                 intArray[j + 1] = temp;
  55.             }
  56.  
  57.             Console.WriteLine("==========Integer Array OutPut===============");
  58.             for (int i = 0; i < intArray.Length; i++)
  59.             {
  60.                 Console.WriteLine(intArray[i]);
  61.             }
  62.         }
  63.  
  64.         //Output: Pass Un-sorted Integer Array to the above Method and get the array Sorted.
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement