Selection Sort
Java
Implementation of Selection Sort
SelectionSort.java
public class SelectionSort {
public static int[] input;
public static int length;
public static void main(String[] args) {
String inputString = "4143675351981074";
length = inputString.length();
input = new int[length];
for (int i = 0; i < length; i++) {
input[i] = Character.getNumericValue(inputString.charAt(i));
}
System.out.println("Unsorted list:");
printList();
System.out.println();
for (int i = 0; i < length; i++) {
System.out.println("Iterating:\n");
int index = findLowest(i);
int value = input[findLowest(i)];
if (index != i) {
for (int j = index; j > i; j--) {
input[j] = input[j - 1];
printList();
}
input[i] = value;
printList();
}
System.out.println();
}
System.out.println("Sorted list:");
printList();
}
public static int findLowest(int startingValue) {
int lowestValue = input[startingValue];
int lowestIndex = startingValue;
for (int i = startingValue; i < length; i++) {
if (input[i] < lowestValue) {
lowestValue = input[i];
lowestIndex = i;
}
}
return lowestIndex;
}
public static void printList() {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + " ");
}
System.out.println();
}
}
C++
Implementation of Selection Sort
SelectionSort.cpp
#include <iostream>
using namespace std;
int findLowest(int startingValue);
void printList();
int *input;
int length;
int main() {
string inputString = "4143675351981074";
length = inputString.length();
input = new int[length];
int i;
for (i = 0; i < length; i++) {
input[i] = inputString.at(i) - '0';
}
cout << "Unsorted list:\n";
printList();
cout << "\n";
for (i = 0; i < length; i++) {
cout << "Iterating:\n";
int index = findLowest(i);
int value = input[findLowest(i)];
if (index != i) {
int j;
for (int j = index; j > i; j--) {
input[j] = input[j - 1];
printList();
}
input[i] = value;
printList();
}
cout << "\n";
}
cout << "Sorted list:\n";
printList();
return 0;
}
int findLowest(int startingValue) {
int lowestValue = input[startingValue];
int lowestIndex = startingValue;
int i;
for (i = startingValue; i < length; i++) {
if (input[i] < lowestValue) {
lowestValue = input[i];
lowestIndex = i;
}
}
return lowestIndex;
}
void printList() {
int i;
for (i = 0; i < length; i++) {
cout << input[i] << " ";
}
cout << "\n";
}
C#
Implementation of Selection Sort
SelectionSort.cs
using System;
public class SelectionSort
{
public static int[] input;
public static int length;
public static void Main()
{
String inputString = "4143675351981074";
length = inputString.Length;
input = new int[length];
for (int i = 0; i < length; i++)
{
input[i] = (int)Char.GetNumericValue(inputString[i]);
}
Console.WriteLine("Unsorted list:");
printList();
Console.WriteLine();
for (int i = 0; i < length; i++)
{
Console.WriteLine("Iterating:\n");
int index = findLowest(i);
int value = input[findLowest(i)];
if (index != i)
{
for (int j = index; j > i; j--)
{
input[j] = input[j - 1];
printList();
}
input[i] = value;
printList();
}
Console.WriteLine();
}
Console.WriteLine("Sorted list:");
printList();
}
public static int findLowest(int startingValue)
{
int lowestValue = input[startingValue];
int lowestIndex = startingValue;
for (int i = startingValue; i < length; i++)
{
if (input[i] < lowestValue)
{
lowestValue = input[i];
lowestIndex = i;
}
}
return lowestIndex;
}
public static void printList()
{
for (int i = 0; i < input.Length; i++)
{
Console.Write(input[i] + " ");
}
Console.WriteLine();
}
}
Console Output
SelectionSort
Unsorted list:
4 1 4 3 6 7 5 3 5 1 9 8 1 0 7 4
Iterating:
4 1 4 3 6 7 5 3 5 1 9 8 1 1 7 4
4 1 4 3 6 7 5 3 5 1 9 8 8 1 7 4
4 1 4 3 6 7 5 3 5 1 9 9 8 1 7 4
4 1 4 3 6 7 5 3 5 1 1 9 8 1 7 4
4 1 4 3 6 7 5 3 5 5 1 9 8 1 7 4
4 1 4 3 6 7 5 3 3 5 1 9 8 1 7 4
4 1 4 3 6 7 5 5 3 5 1 9 8 1 7 4
4 1 4 3 6 7 7 5 3 5 1 9 8 1 7 4
4 1 4 3 6 6 7 5 3 5 1 9 8 1 7 4
4 1 4 3 3 6 7 5 3 5 1 9 8 1 7 4
4 1 4 4 3 6 7 5 3 5 1 9 8 1 7 4
4 1 1 4 3 6 7 5 3 5 1 9 8 1 7 4
4 4 1 4 3 6 7 5 3 5 1 9 8 1 7 4
0 4 1 4 3 6 7 5 3 5 1 9 8 1 7 4
Iterating:
0 4 4 4 3 6 7 5 3 5 1 9 8 1 7 4
0 1 4 4 3 6 7 5 3 5 1 9 8 1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