New User
New User

Reputation: 29

Why would this insertion sort return an index out of range exception?

For some reason, the code below for a C# insertion sort is returning an index out of range exception. I would've tried to write out each variable to the console but the exception isn't letting me. I can't find a solution so help appreciated.

using System;
using System.Collections.Generic;

class MainClass {
    public static void Main (string[] args) {
        int[] unsortedArray = {23, 19, 21, 44, 40, 60, 73, 80, 38, 55, 29, 78, 83, 61, 63, 9, 93, 6, 51, 11};
        //Sets the unsorted list

        Console.WriteLine ("Insertion Sort");
        for (int i = 0; i < 20; i++) {
          Console.Write(unsortedArray[i] + " , ");
        }
        //Displays a welcome message and the unsorted list

        Console.WriteLine();
        Console.WriteLine();
        //Makes a gap between the unsorted and sorted list

        List<int> sortedArray = new List<int>();
        //Creates a new list for the sorted list

        for (int i = 0; i < 19; i++) {
          if (unsortedArray[i] < unsortedArray[i + 1]) {
            sortedArray[i] = unsortedArray[i];
            //If the next item in the unsorted list is less than or equal to the one after,
            //it is added to the next spot in the sorted list.
          }
          else if (unsortedArray[i] > unsortedArray[i + 1]) {
            sortedArray[i] = unsortedArray[i + 1];
            //If the next item in the unsorted list is greater than the one after, it is
            //moved behind one place and added to the sorted list before.
          }
        }


        for (int i = 0; i < 19; i++) {
          Console.Write(sortedArray[i] + ", ");
          //Displays the sorted array
        }

    }
}

Upvotes: 2

Views: 290

Answers (3)

Alex
Alex

Reputation: 18556

This is probably your error:

List<int> sortedArray = new List<int>();
// ..
sortedArray[i] = // ..

You cannot assign a value to a List with an index without any previous assignment. You either need to change your list to an array or use add instead.

Also: you should not name list objects "array", it only confuses people.

Upvotes: 6

Aldert
Aldert

Reputation: 4323

Maybe you want to write your own logic for learning purpose, but: Just for you to know, the framework supports sorting:

        int[] unsortedArray = { 23, 19, 21, 44, 40, 60, 73, 80, 38, 55, 29, 78, 83, 61, 63, 9, 93, 6, 51, 11 };

        List<int> sortedArray = new List<int>(unsortedArray);
        sortedArray.Sort();

Upvotes: 0

kkica
kkica

Reputation: 4104

As said in Alex Answer you cannot assign a value to a List with an index without any previous assignment.

Also, your logic is entirely wrong. You need 2 loops. First loop that iterates the members of the unsorted array. Second loop that iterates the members of the sorted list. Please read more here

The algorithm is as follows.

For each element of the unsorted array

    Find the position that the element in the sorted array (list in your case), by looping the sorted list and comparing the values
    Push all the elements after that position, one index.
    Insert the element in the position specified

I will not give you the code. Although there is a solution for c# in the link. However, i suggest you try to solve it by understanding the algorithm

Upvotes: 0

Related Questions