Insertion Sort Algorithm Implementation in C# with Flowchart, Essays (high school) of Computer science

The source code of an insertion sort algorithm implemented in c#. The code includes two methods: 'insertionsort' and 'insertionsortbyshift'. The 'main' method initializes an integer array, prints the original and sorted arrays, and calls the 'insertionsort' method. A flowchart is also included to illustrate the logic of the insertion sort algorithm.

Typology: Essays (high school)

2019/2020

Uploaded on 03/04/2022

joshua13
joshua13 🇬🇧

11 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Insertion Sort Coding in C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CommonInsertion_Sort
{
class Program
{
static void Main(string[] args)
{
int[] numbers = new int[10] {2, 5, -4, 11, 0, 18, 22, 67, 51,
6};
Console.WriteLine("\nOriginal Array Elements :");
PrintIntegerArray(numbers);
Console.WriteLine("\nSorted Array Elements :");
PrintIntegerArray(InsertionSort(numbers));
Console.WriteLine("\n");
}
static int[] InsertionSort(int[] inputArray)
{
for (int i = 0; i < inputArray.Length - 1; i++)
{
for (int j = i + 1; j > 0; j--)
{
if (inputArray[j - 1] > inputArray[j])
{
int temp = inputArray[j - 1];
inputArray[j - 1] = inputArray[j];
inputArray[j] = temp;
}
}
}
return inputArray;
}
public static void PrintIntegerArray(int[] array)
{
foreach (int i in array)
{
Console.Write(i.ToString() + " ");
}
}
public static int[] InsertionSortByShift(int[] inputArray)
{
for (int i = 0; i < inputArray.Length - 1; i++)
{
int j;
var insertionValue = inputArray[i];
for (j = i; j > 0; j--)
{
if (inputArray[j - 1] > insertionValue)
{
inputArray[j] = inputArray[j - 1];
}
pf3

Partial preview of the text

Download Insertion Sort Algorithm Implementation in C# with Flowchart and more Essays (high school) Computer science in PDF only on Docsity!

Insertion Sort Coding in C#

using System; using System.Collections.Generic; using System.Linq; using System.Text;

namespace CommonInsertion_Sort { class Program { static void Main(string[] args) { int[] numbers = new int[10] {2, 5, -4, 11, 0, 18, 22, 67, 51, 6}; Console.WriteLine("\nOriginal Array Elements :"); PrintIntegerArray(numbers); Console.WriteLine("\nSorted Array Elements :"); PrintIntegerArray(InsertionSort(numbers)); Console.WriteLine("\n"); }

static int[] InsertionSort(int[] inputArray) { for (int i = 0; i < inputArray.Length - 1; i++) { for (int j = i + 1; j > 0; j--) { if (inputArray[j - 1] > inputArray[j]) { int temp = inputArray[j - 1]; inputArray[j - 1] = inputArray[j]; inputArray[j] = temp; } } } return inputArray; } public static void PrintIntegerArray(int[] array) { foreach (int i in array) { Console.Write(i.ToString() + " "); } }

public static int[] InsertionSortByShift(int[] inputArray) { for (int i = 0; i < inputArray.Length - 1; i++) { int j; var insertionValue = inputArray[i]; for (j = i; j > 0; j--) { if (inputArray[j - 1] > insertionValue) { inputArray[j] = inputArray[j - 1]; }

inputArray[j] = insertionValue; } return inputArray; }