Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Selection sort
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { /* Selection sort is an algorithm of sorting an array where it loop from the start of the loop, and check through other elements to find the minimum value. After the end of the first iteration, the minimum value is swapped with the current element. The iteration then continues from the 2nd element and so on. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list. The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary by one element to the right. This algorithm is not suitable for large data sets as its average and worst case complexities are of Ο(n2), where n is the number of items. */ Console.WriteLine("Hello, world!"); int [] arr = getList(); Console.WriteLine("Unsorted array : "); Display(arr); InsertionSort(arr); Console.WriteLine(); Console.WriteLine("Sorted array : "); Display(arr); } static void InsertionSort(int []arr) { int size = arr.Length; // array loop for (int indx = 0; indx < size - 1; indx++) { int min_idx = indx; // Find the minimum element in unsorted array for (int j = indx + 1; j < size; j++) if (arr[j] < arr[min_idx]) min_idx = j; // Swap the minimum element with the current element int temp = arr[min_idx]; arr[min_idx] = arr[indx]; arr[indx] = temp; } } public static int[] getList(){ int [] nums = { 8,4,6,3,9,2 }; return nums; } // Display the array static void Display(int []arr) { int size = arr.Length; for (int indx=0; indx < size; ++indx) Console.Write(arr[indx] + " "); Console.WriteLine(); } } }
run
|
edit
|
history
|
help
0
Math 10.96
Date Time Conversion based on time zone
1 -5 6 x1=3 x2=2
abc
pankaj_sql
adxasaxsdf
Ejercicio 1 clase 8 feb
hamming distance
Shorten SMS Text
Game 1.0.4