Created
February 19, 2020 01:44
-
-
Save Skalnark/1988689b969f91c860ab6d46a774ce0b to your computer and use it in GitHub Desktop.
Sorting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
class Sort { | |
static void InsertionSort(int[] arr) | |
{ | |
int n = arr.Length; | |
for (int i = 1; i < n; ++i) | |
{ | |
int key = arr[i]; | |
int j = i - 1; | |
while (j >= 0 && arr[j] > key) | |
{ | |
arr[j + 1] = arr[j]; | |
j = j - 1; | |
} | |
arr[j + 1] = key; | |
} | |
} | |
static void SelectionSort(int []arr) | |
{ | |
int n = arr.Length; | |
for (int i = 0; i < n - 1; i++) | |
{ | |
int min_idx = i; | |
for (int j = i + 1; j < n; j++) | |
if (arr[j] < arr[min_idx]) | |
min_idx = j; | |
int temp = arr[min_idx]; | |
arr[min_idx] = arr[i]; | |
arr[i] = temp; | |
} | |
} | |
static void Show(int[] arr) | |
{ | |
int n = arr.Length; | |
for (int i = 0; i < n; ++i) | |
Console.Write(arr[i] + " "); | |
Console.Write("\n"); | |
} | |
static void BubbleSort(int []arr) | |
{ | |
int n = arr.Length; | |
for (int i = 0; i < n - 1; i++) | |
for (int j = 0; j < n - i - 1; j++) | |
if (arr[j] > arr[j + 1]) | |
{ | |
int temp = arr[j]; | |
arr[j] = arr[j + 1]; | |
arr[j + 1] = temp; | |
} | |
} | |
public static void Main() | |
{ | |
int[] arr1 = { 12, 11, 13, 5, 6 }; | |
int[] arr2 = { 12, 13, 11, 6, 5 }; | |
int[] arr3 = { 6, 5, 12, 11, 13 }; | |
InsertionSort(arr1); | |
Show(arr1); | |
SelectionSort(arr2); | |
Show(arr2); | |
BubbleSort(arr3); | |
Show(arr3); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment