Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Insertion_Random
{
    public class Insertionsort
    {
        private int[] item;

        public Insertionsort(int[]i)
        {
            item = i;
        }
        //Return the size of the array of data to sort
        public static int GetSize()
        {
            Console.WriteLine("Enter the number of data items: ");
            return int.Parse(Console.ReadLine());
        }
        /* Return the data array , either
         * entered by the user,or
         * randomly selected.
         *
         */
        public static int[] GetData(int size)
        {
            int[] item = new int[size];
            Console.Write("Enter'Y' to enter data,'N'for random data: ");
            string enter = Console.ReadLine();
            if (enter.ToUpper() == "Y")
                for (int i = 0; i < size; i++)
                {
                    Console.Write("Enter item[" + i + "]: ");
                    item[i] = int.Parse(Console.ReadLine());
                }
            else
            {
                Random random = new Random();
                for (int i = 0; i < size; i++)
                    item[i] = random.Next(100);
            }
            return item;
        }
        /*Insert the next element in the correct position
         * with respect to itn predecensors.
         */
        public void InsertNext(int i)
        {
            int current = item[i];
            int j = 0;
            while(current>item[j])j++;
            for (int k = i;k>j;k--)
                item[k] = item[k-1];
            item[j]=current;
        }
        //Inserts each element
        public void Sort()
        {
            for (int i = 1; i < item.Length; i++)
            {
                InsertNext(i);
            }
        }
        //Gets and sorts an array of integer data.
        public static void Main()
        {
            int size = GetSize();
            int[] item = GetData(size);
            
            ArrayCopy.Display("The data to sort is ", item);
            Insertionsort s = new Insertionsort(item);
            s.Sort();
            ArrayCopy.Display("The sort data is ", item);



        }





    }
}
Posted
Comments
PingpinkApple 6-Feb-12 23:29pm    
I'm writing an insertion number code between 200-1000 , but there are some mistakes that the program can't be run. Is there any suggestions to solve the code? ,
Thanks so much.
LanFanNinja 6-Feb-12 23:55pm    
Post the code for the ArrayCopy class or maybe even just the code for the ArrayCopy.Display() method as this seems to be where the problem is as pointed out in solution 1.
Sergey Alexandrovich Kryukov 7-Feb-12 0:03am    
Not a question.
--SA
LanFanNinja 7-Feb-12 0:11am    
It also seems no one debugs there code these days! Has it gone out of style? :)

1 solution

I just modified your main block like follows it is working


 int size = GetSize();
 int[] item = GetData(size);

// ArrayCopy.Display("The data to sort is ", item);
 Insertionsort s = new Insertionsort(item);
 s.Sort();
 //ArrayCopy.Display("The sort data is ", item);

 item = s.item;
 for (int i = 0; i < item.Length; i++)
     Console.WriteLine(item[i]);
 Console.ReadLine();
 
Share this answer
 
Comments
LanFanNinja 6-Feb-12 23:53pm    
+5 I did the same and it works fine. I also used two for loops to display the items array before and after sort and it is indeed sorting properly.

TO THE OP:
So the problem must be in your ArrayCopy class and we will need to see the source code for that to figure out the problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900