zornitza_gencheva

Task 7

Jul 6th, 2013
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SelectionSortAlgoritam
  8. {
  9.     class SelectionSortAlgoritam
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             //Task 7: Sorting an array means to arrange its elements in increasing order.
  14.             //Write a program to sort an array. Use the "selection sort" algorithm:
  15.             //Find the smallest element, move it at the first position, find
  16.             //the smallest from the rest, move it at the second position, etc.
  17.  
  18.             Console.WriteLine("Please insert 'N' integer number: ");
  19.             int N = int.Parse(Console.ReadLine());
  20.             int[] numbers = new int[N];
  21.  
  22.             Console.WriteLine("Please insert 'N' elements in the array: ");
  23.             for (int i = 0; i < N; i++)
  24.             {
  25.                 numbers[i] = int.Parse(Console.ReadLine());
  26.             }
  27.  
  28.             int temp = 0;
  29.  
  30.             for (int j = 0; j < N; j++) //"Selection sort" solution.
  31.             {
  32.                 for (int i = j + 1; i < N; i++)
  33.                 {
  34.                     if (numbers[j] > numbers[i])
  35.                     {
  36.                         temp = numbers[i];
  37.                         numbers[i] = numbers[j];
  38.                         numbers[j] = temp;
  39.                     }
  40.                 }
  41.             }
  42.            
  43.  
  44.             //int temp = 0; //"Bubble sort" solution follows.
  45.             //int flag = 0;
  46.  
  47.             //for (int j = 0; j < N; j++)
  48.             //{
  49.             //    for (int i = 0; i < N - 1; i++)
  50.             //    {
  51.             //        if (numbers[i] > numbers[i + 1])
  52.             //        {
  53.             //            temp = numbers[i];
  54.             //            numbers[i] = numbers[i + 1];
  55.             //            numbers[i + 1] = temp;
  56.             //            flag++;
  57.             //        }
  58.             //    }
  59.  
  60.             //    if (flag == 0)
  61.             //    {
  62.             //        break;
  63.             //    }
  64.                
  65.             //    flag = 0;
  66.             //}
  67.  
  68.             Console.WriteLine("The elements of array in increasing order: ");
  69.             for (int i = 0; i < numbers.Length; i++)
  70.             {
  71.                 Console.WriteLine(numbers[i]);
  72.             }
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment