Consider the following unsorted list of integers 8 3 1 12 5 9 a) Show the content of the array after each pass when using the Bubble Sort, to sort the list in ascending order. b) Show the content of the array after each pass when using the Selection Sort, to sort the list in descending order. c) Show the content of the array after each pass when using the Insertion Sort, to sort the list in ascending order.
using System;
using System.Collections.Generic;
namespace App
{
class Program
{
static void BubbleSort(int[] integers)
{
bool flag = true;
int temp;
int step=1;
//sorting an array
for (int i = 1; (i <= (integers.Length - 1)) && flag; i++)
{
flag = false;
for (int j = 0; j < (integers.Length - 1); j++)
{
if (integers[j + 1] < integers[j])
{
temp = integers[j];
integers[j] = integers[j + 1];
integers[j + 1] = temp;
flag = true;
}
Console.WriteLine("Step {0}", step);
for (int k = 0; k < integers.Length; k++)
{
Console.Write("{0} ", integers[k]);
}
step++;
Console.WriteLine();
}
}
}
static void SelectionSort(int[] integersSelectionSort)
{
//pos_min is short for position of min
int pos_min, temp;
int step = 1;
for (int i = 0; i < integersSelectionSort.Length - 1; i++)
{
pos_min = i;//set pos_min to the current index of array
for (int j = i + 1; j < integersSelectionSort.Length; j++)
{
if (integersSelectionSort[j] > integersSelectionSort[pos_min])
{
//pos_min will keep track of the index that min is in, this is needed when a swap happens
pos_min = j;
}
}
//if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
if (pos_min != i)
{
temp = integersSelectionSort[i];
integersSelectionSort[i] = integersSelectionSort[pos_min];
integersSelectionSort[pos_min] = temp;
}
Console.WriteLine("Step {0}", step);
for (int k = 0; k < integersSelectionSort.Length; k++)
{
Console.Write("{0} ", integersSelectionSort[k]);
}
step++;
Console.WriteLine();
}
}
private static void InsertionSort(int[] integersSelectionSort)
{
int step = 1;
for (int i = 1; i < integersSelectionSort.Length; i++)
{
int val = integersSelectionSort[i];
for (int j = i - 1; j >= 0; )
{
if (val < integersSelectionSort[j])
{
integersSelectionSort[j + 1] = integersSelectionSort[j];
j--;
integersSelectionSort[j + 1] = val;
}
else
{
break;
}
Console.WriteLine("Step {0}", step);
for (int k = 0; k < integersSelectionSort.Length; k++)
{
Console.Write("{0} ", integersSelectionSort[k]);
}
Console.WriteLine();
step++;
}
}
}
static void Main(string[] args)
{
int[] integersBubbleSort = { 8, 3, 1, 12, 5, 9 };
Console.WriteLine("Current array:");
for (int i = 0; i < integersBubbleSort.Length; i++)
{
Console.Write("{0} ", integersBubbleSort[i]);
}
Console.WriteLine(Environment.NewLine + "Bubble Sort: ");
BubbleSort(integersBubbleSort);
Console.WriteLine(Environment.NewLine + "Sorted array");
for (int i = 0; i < integersBubbleSort.Length; i++)
{
Console.Write("{0} ", integersBubbleSort[i]);
}
int[] integersSelectionSort = { 8, 3, 1, 12, 5, 9 };
Console.WriteLine(Environment.NewLine + Environment.NewLine + "Current array:");
for (int i = 0; i < integersSelectionSort.Length; i++)
{
Console.Write("{0} ", integersSelectionSort[i]);
}
Console.WriteLine(Environment.NewLine + Environment.NewLine + "Selection Sort: ");
SelectionSort(integersSelectionSort);
Console.WriteLine(Environment.NewLine + "Sorted array");
for (int i = 0; i < integersSelectionSort.Length; i++)
{
Console.Write("{0} ", integersSelectionSort[i]);
}
int[] integersInsertionSort = { 8, 3, 1, 12, 5, 9 };
Console.WriteLine(Environment.NewLine + Environment.NewLine + "Current array:");
for (int i = 0; i < integersInsertionSort.Length; i++)
{
Console.Write("{0} ", integersInsertionSort[i]);
}
Console.WriteLine(Environment.NewLine + Environment.NewLine + "Insertion Sort: ");
InsertionSort(integersInsertionSort);
Console.WriteLine(Environment.NewLine + "Sorted array");
for (int i = 0; i < integersInsertionSort.Length; i++)
{
Console.Write("{0} ", integersInsertionSort[i]);
}
Console.ReadLine();
}
}
}
Comments
Leave a comment