简体   繁体   中英

Insertion Sort in C#

I'm trying to perform a simple insertion sort in C# but I don't know what I'm doing wrong. When I run my program I want it to output a window like this:

before sorting
80 60 40 20 10 30 50 70

 during sorting
60 80 40 20 10 30 50 70
40 60 80 20 10 30 50 70
20 40 60 80 10 30 50 70
10 20 40 60 80 30 50 70
10 20 30 40 60 80 50 70
10 20 30 40 50 60 80 70
10 20 30 40 50 60 70 80

after sorting
10 20 30 40 50 60 70 80

But it's just showing only the after sorting part. What might I be doing wrong?

This is the code:

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

namespace ConsoleApplication1
{
    class Program
    {
        class CArray
        {
            private int[] arr;
            private int upper;
            private int numElements;
            public CArray(int size)
            {
                arr = new int[size];
                upper = size - 1;
                numElements = 0;

            }
            public void insert(int item)
            {
                arr[numElements] = item;
                numElements++;
            }
            public void displayElements()
            {
                for (int i = 0; i <= upper; i++)
                    Console.Write(arr[i] + " ");
                Console.ReadKey();

            }
            public void clear()
            {
                for (int i = 0; i <= upper; i++)
                    arr[i] = 0;
                numElements = 0;

            }

            public void insertionsort() {
            int inner, temp;
            for (int outer = 1; outer <= upper; outer++) { 
              temp = arr[outer];
              inner = outer;
              while (inner > 0 && arr[inner - 1] >= temp) { 
              arr[inner] = arr[inner-1];
              inner -= 1;
              }
                arr[inner] = temp;

            }

        }
        }
        static void Main(string[] args)
        {

            CArray nums = new CArray(10);
            Random rnd = new Random(100);
            for (int i = 0; i < 10; i++)

                nums.insert((int)(rnd.NextDouble() * 100));

            Console.WriteLine("before sorting: ");
            nums.displayElements();

            Console.WriteLine("during sorting: ");
            nums.insertionsort();

            Console.WriteLine("after sorting: ");
            nums.displayElements();

        }
    }
}

您的insertionSort()函数不会向显示器输出任何内容。

在 insertsort() 中的某处调用 displayElements() 怎么样...?

This works for me...

public void insertionsort() { 
    int inner, temp; 

    for (int outer = 1; outer <= upper; outer++) {  
        displayElements();                          
        Console.WriteLine(); 

        temp = arr[outer]; 
        inner = outer; 
        while (inner > 0 && arr[inner - 1] >= temp) {  
            arr[inner] = arr[inner-1]; 
            inner -= 1;
        } 

        arr[inner] = temp; 
    } 

Here is the output I get when I add the two lines to your insertsort() method...

before sorting:
96 15 66 90 35 94 71 61 34 14

during sorting:
96 15 66 90 35 94 71 61 34 14
15 96 66 90 35 94 71 61 34 14
15 66 96 90 35 94 71 61 34 14
15 66 90 96 35 94 71 61 34 14
15 35 66 90 96 94 71 61 34 14
15 35 66 90 94 96 71 61 34 14
15 35 66 71 90 94 96 61 34 14
15 35 61 66 71 90 94 96 34 14
15 34 35 61 66 71 90 94 96 14

after sorting:
14 15 34 35 61 66 71 90 94 96

using System;

namespace InsertionSort
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] num = { 90, 110, 80, 30, 10, 20 };
            for (int i = 1; i < num.Length; i++)
            {
                int tmp = num[i];
                for (int j = i - 1; j >= 0; j--)
                {
                    int tmpIndex = j + 1;
                    if (tmp < num[j])
                    {
                        num[tmpIndex] = num[j];
                        num[j] = tmp;
                    }
                }
            }
            //Write the sorted list numbers
            for (int i = 0; i < num.Length; i++)
            {
                Console.WriteLine(i);
            }            }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM