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

namespace selectionsort
{
    class List
    {
        public int[] arr = new int[10];
        public int[] arr1 = new int[10];
        public int n,h1;
        public void read()
        {
            Console.WriteLine("array size should be less than or equal to 10");
            Console.Write("enter the array size ");
            n = Convert.ToInt32(Console.ReadLine());
            h1 = n - 1;
            if (n > 10)
            {
                Console.WriteLine("\nArray can have maximum 10 elements.\n");
            }
            else
            {
                Console.WriteLine("");
                Console.WriteLine("-------------");
                Console.WriteLine("Enter Array Elements");
                Console.WriteLine("-----------------");

                for (int i = 0; i < n; i++)
                {
                    Console.Write("<" + (i + 1) + "> ");
                    arr[i] = Convert.ToInt32(Console.ReadLine());
                }

            }
        }

        public void display()
        {
            Console.WriteLine("");
            Console.WriteLine("----------------");
            Console.WriteLine("Sorted Array Elements");
            Console.WriteLine("-----------------");

            for (int j = 0; j < n; j++)
            {
                Console.Write("\t" + arr[j]);
            }

        }
        public void bubblesortarray()
        {
            //this loop used for pass
            for (int i = 1; i < n; i++)
            {
                //this loop used for swap the value 
                for (int j = 0; j < n - i; j++)
                {
                    if (arr[j + 1] < arr[j])
                    {
                        int temp;
                        temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }

                }
            }
        }
        public void selectionsortarray()
        {
            //this loop used for pass
            for (int i = 0; i < n - 1; i++)
            {
                //this loop used for find the minimum value
                int min = i;
                for (int j = i + 1; j < n; j++)
                {
                    if (arr[j] < arr[min])
                    {
                        min = j;
                    }

                }
                int temp = arr[min];
                arr[min] = arr[i];
                arr[i] = temp;
            }
        }
        public void insertionsort()
        {
            for (int i = 1; i < n; i++)
            {
                int temp = arr[i];
                int j = i - 1;
                while ((j >= 0) && (arr[j] > temp))
                {
                    arr[j + 1] = arr[j];
                    j--;
                }
                arr[j + 1] = temp;
            }
        }
        void shellsort()
        {
            //this loop used for increment
            for (int inc = 3; inc >= 1; inc--)
            {
                //this loop used for make the list
                for (int l = 0; l <= inc - 1; l++)
                {
                    //this loop used for compare two values
                    for (int i = l + inc; i < n; i += inc)
                    {
                        int temp = arr[i];
                        int j = i - inc;
                        //this loop used for swap the value
                        while (j >= 0 && arr[j] > temp)
                        {
                            temp = arr[j];
                            arr[j] = arr[j + inc];
                            arr[j + inc] = temp;
                            temp = arr[j];
                            j -= inc;
                        }
                    }
                }
            }
        }
       public void QuickSort(int l,int h)
        {
            if (l > h)
            {
                return;
            }
            int pivot = arr[l];
            int i = l + 1;
            int j = h;
            while (i <= j)
            {
                while (i <=h && arr[i] <= pivot)
                {
                    i++;
                }
                while (j >= l && arr[j] > pivot)
                {
                    j--;
                }
                if (i < j)
                {
                    int temp;
                    temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
            if (l < j)
            {
                int t = arr[l];
                arr[l]=arr[j];
                arr[j]=t;
            }
            QuickSort(l, j - 1);
            QuickSort(j + 1, h);
        }
        public void MergeSort(int l, int h)
        {
            if (l >= h)
            {
                return;
            }
            int mid = (l + h) / 2;
            MergeSort(l, h);
            MergeSort(mid + 1, h);
            int i = l;
            int j = mid + 1;
            int k = l;
            while (i <= mid || j <= h)
            {
                if (arr[i] <= arr[j])
                {
                    arr1[k] = arr[i];
                    i++;
                }
                else
                {
                    arr1[k] = arr[j];
                    j++;
                }
            }
            while (j <= h)
            {
                arr1[k] = arr[j];
                j++;
                k++;
            }
            while (i <= mid)
            {
                arr[k] = arr[i];
                i++;
                k++;
            }
            for (i = 0; i < h1; i++)
            {
                arr[i] = arr1[j];
            }
        }
        public static void Main(string[] args)
        {
            List ob = new List();
            Console.WriteLine("1 for bubble,2 for insertionsort,3 for selection,4 for shellsort,5 for QuickSort,6 for mergesort,7 for exist");
            int c = Convert.ToInt32(Console.ReadLine());
            switch (c)
            {
                case 1:
                    {
                        ob.read();
                        ob.bubblesortarray();
                        ob.display();
                        break;
                    }
                case 2:
                    {
                        ob.read();
                        ob.insertionsort();
                        ob.display();
                        break;
                    }
                case 3:
                    {
                        ob.read();
                        ob.selectionsortarray();
                        ob.display();
                        break;
                    }
                case 4:
                    {
                        ob.read();
                        ob.shellsort();
                        ob.display();
                        break;
                    }
                case 5:
                    {
                        ob.read();
                        int high = ob.h1;
                        int low = 0;
                        ob.QuickSort(low, high);
                        ob.display();
                        break;
                    }
                case 6:
                    {
                        ob.read();
                        int high = ob.h1;
                        int low = 0;
                        ob.MergeSort(low, high);
                        ob.display();
                        break;
                    }
                case 7:
                    {
                        Environment.Exit(0);
                        break;
                    }
                default:
                    {
                        Console.WriteLine("123.........");
                        break;
                    }
            }
            Console.ReadLine();
        }
    }
}

Recommended Answers

All 10 Replies

Please wrap your code between code tages [code] your code here... [/code]
Mention the line of code it breaks on

How are you calling your code? I see the menu prompt with items 1-7, then you enter an array size. What sort method is causing the issue and with what array items?

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

namespace selectionsort
{
class List
{
public int[] arr = new int[10];
public int[] arr1 = new int[10];
public int n,h1;
public void read()
{
Console.WriteLine("array size should be less than or equal to 10");
Console.Write("enter the array size ");
n = Convert.ToInt32(Console.ReadLine());
h1 = n - 1;
if (n > 10)
{
Console.WriteLine("\nArray can have maximum 10 elements.\n");
}
else
{
Console.WriteLine("");
Console.WriteLine("-------------");
Console.WriteLine("Enter Array Elements");
Console.WriteLine("-----------------");

for (int i = 0; i < n; i++)
{
Console.Write("<" + (i + 1) + "> ");
arr[i] = Convert.ToInt32(Console.ReadLine());
}

}
}

public void display()
{
Console.WriteLine("");
Console.WriteLine("----------------");
Console.WriteLine("Sorted Array Elements");
Console.WriteLine("-----------------");

for (int j = 0; j < n; j++)
{
Console.Write("\t" + arr[j]);
}

}
public void bubblesortarray()
{
//this loop used for pass
for (int i = 1; i < n; i++)
{
//this loop used for swap the value 
for (int j = 0; j < n - i; j++)
{
if (arr[j + 1] < arr[j])
{
int temp;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}

}
}
}
public void selectionsortarray()
{
//this loop used for pass
for (int i = 0; i < n - 1; i++)
{
//this loop used for find the minimum value
int min = i;
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[min])
{
min = j;
}

}
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
}
}
public void insertionsort()
{
for (int i = 1; i < n; i++)
{
int temp = arr[i];
int j = i - 1;
while ((j >= 0) && (arr[j] > temp))
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = temp;
}
}
void shellsort()
{
//this loop used for increment
for (int inc = 3; inc >= 1; inc--)
{
//this loop used for make the list
for (int l = 0; l <= inc - 1; l++)
{
//this loop used for compare two values
for (int i = l + inc; i < n; i += inc)
{
int temp = arr[i];
int j = i - inc;
//this loop used for swap the value
while (j >= 0 && arr[j] > temp)
{
temp = arr[j];
arr[j] = arr[j + inc];
arr[j + inc] = temp;
temp = arr[j];
j -= inc;
}
}
}
}
}
public void QuickSort(int l,int h)
{
if (l > h)
{
return;
}
int pivot = arr[l];
int i = l + 1;
int j = h;
while (i <= j)
{
while (i <=h && arr[i] <= pivot)
{
i++;
}
while (j >= l && arr[j] > pivot)
{
j--;
}
if (i < j)
{
int temp;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
if (l < j)
{
int t = arr[l];
arr[l]=arr[j];
arr[j]=t;
}
QuickSort(l, j - 1);
QuickSort(j + 1, h);
}
public void MergeSort(int l, int h)
{
if (l >= h)
{
return;
}
int mid = (l + h) / 2;
MergeSort(l, h);
MergeSort(mid + 1, h);
int i = l;
int j = mid + 1;
int k = l;
while (i <= mid || j <= h)
{
if (arr[i] <= arr[j])
{
arr1[k] = arr[i];
i++;
}
else
{
arr1[k] = arr[j];
j++;
}
}
while (j <= h)
{
arr1[k] = arr[j];
j++;
k++;
}
while (i <= mid)
{
arr[k] = arr[i];
i++;
k++;
}
for (i = 0; i < h1; i++)
{
arr[i] = arr1[j];
}
}
public static void Main(string[] args)
{
List ob = new List();
Console.WriteLine("1 for bubble,2 for insertionsort,3 for selection,4 for shellsort,5 for QuickSort,6 for mergesort,7 for exist");
int c = Convert.ToInt32(Console.ReadLine());
switch (c)
{
case 1:
{
ob.read();
ob.bubblesortarray();
ob.display();
break;
}
case 2:
{
ob.read();
ob.insertionsort();
ob.display();
break;
}
case 3:
{
ob.read();
ob.selectionsortarray();
ob.display();
break;
}
case 4:
{
ob.read();
ob.shellsort();
ob.display();
break;
}
case 5:
{
ob.read();
int high = ob.h1;
int low = 0;
ob.QuickSort(low, high);
ob.display();
break;
}
case 6:
{
ob.read();
int high = ob.h1;
int low = 0;
ob.MergeSort(low, high);
ob.display();
break;
}
case 7:
{
Environment.Exit(0);
break;
}
default:
{
Console.WriteLine("123.........");
break;
}
}
Console.ReadLine();
}
}
}


when i call the mergesort function than array out of bound error genrates
pleaze give me best solution..............

Is it so hard to post your code in this format?
Using code tags I believe this is your MergeSort function :

public void MergeSort(int l, int h)
        {
            if (l >= h)
            {
                return;
            }
            int mid = (l + h) / 2;
            MergeSort(l, h);
            MergeSort(mid + 1, h);
            int i = l;
            int j = mid + 1;
            int k = l;
            while (i <= mid || j <= h)
            {
                if (arr[i] <= arr[j])
                {
                    arr1[k] = arr[i];
                    i++;
                }
                else
                {
                    arr1[k] = arr[j];
                    j++;
                }
            }
            while (j <= h)
            {
                arr1[k] = arr[j];
                j++;
                k++;
            }
            while (i <= mid)
            {
                arr[k] = arr[i];
                i++;
                k++;
            }
            for (i = 0; i < h1; i++)
            {
                arr[i] = arr1[j];
            }
        }

Please point us the line where your error occurs. Please?

when i run this function array out of bound error generate

public void MergeSort(int l, int h)
{
if (l >= h)
{
return;
}
int mid = (l + h) / 2;
MergeSort(l, h);
MergeSort(mid + 1, h);
int i = l;
int j = mid + 1;
int k = l;
while (i <= mid || j <= h)
{
if (arr[i] <= arr[j])
{
arr1[k] = arr[i];
i++;
}
else
{
arr1[k] = arr[j];
j++;
}
}
while (j <= h)
{
arr1[k] = arr[j];
j++;
k++;
}
while (i <= mid)
{
arr[k] = arr[i];
i++;
k++;
}
for (i = 0; i < h1; i++)
{
arr[i] = arr1[j];
}

i have not disired output plz.......help me ......give me best solution....

using System;
using System.Text;

namespace Merge_Sort
{
    class List
    {
        private int[] arr = new int[20];
        private int[] sorted = new int[20];
        private int cmp_count; //Number of comparisons
        private int mov_count; //Number of data movements

        // Number of elements in array
        private int n;

        public List()
        {
            cmp_count = 0;
            mov_count = 0;
        }

        void read()
        {
            // Get the number of elements to store in the array
            while (true)
            {
                Console.Write("Enter the number of elements in the array: ");
                string s = Console.ReadLine();
                n = Int32.Parse(s);
                if (n <= 20)
                    break;
                else
                    Console.WriteLine("\nArray can have maximum 20 elements.\n");
            }

            Console.WriteLine("-----------------------");
            Console.WriteLine(" Enter array elements  ");
            Console.WriteLine("-----------------------");

            // Get array elements
            for (int i = 0; i < n; i++)
            {
                Console.Write("<{0}> ", i + 1);
                string s1 = Console.ReadLine();
                arr[i] = Int32.Parse(s1);
            }
        }

        public void m_sort(int low, int high)
        {
            int mid;

            if (low >= high)
                return;

            mid = (low + high) / 2;

            m_sort(low, mid);
            m_sort(mid + 1, high);
            merge(low, mid, high);
        }

        public void merge(int low, int mid, int high)
        {
            int i, j, k;

            i = low;
            j = mid + 1;
            k = low;

            while ((i <= mid) && (j <= high))
            {
                if (arr[i] <= arr[j])
                {
                    sorted[k++] = arr[i++];
                }
                else
                {
                    sorted[k++] = arr[j++];
                }
                cmp_count++;
            }

            //If there are still some elements in the first sub list
            //append them to the new list.

            while (i <= mid)
            {
                sorted[k++] = arr[i++];
                mov_count++;
            }

            //If there are still some elements in the second sub list
            //append them to the new list.

            while (j <= high)
            {
                sorted[k++] = arr[j];
                mov_count++;
            }

            //Copy the sorted elements in the original array
            for (i = low; i <= high; i++)
            {
                arr[i] = sorted[i];
                mov_count++;
            }
        }

        int getSize()
        {
            return (n);
        }

        void display()
        {
            Console.WriteLine("-----------------------");
            Console.WriteLine(" Sorted array elements ");
            Console.WriteLine("-----------------------");

            for (int j = 0; j < n; j++)
            {
                Console.WriteLine(arr[j]);
            }

            Console.WriteLine("\nNumber of comparisons: " + cmp_count);
            Console.WriteLine("\nNumber of data movements: " + mov_count);
        }

        static void Main(string[] args)
        {
            // Declaring the object of the class
            List myList = new List();

            // Accept array elements;
            myList.read();

            // Calling the sorting function
            myList.m_sort(0, myList.getSize() - 1); //First call to merge sort algo.

            // Display sorted array
            myList.display();

            // To exit from the console
            Console.WriteLine("\n\nPress Return to exit.");
            Console.Read();
        }

    }
}

AnkitKumar,

What would you expect?

Dear AnkitKumar, please don't open more threads on the same question we are here to help but you should first need to help yourself by answering our questions.
- Please change your font
- Please tell us what line crashes with you and what's the error

merged the two threads.

The while loop in line 96 of post #7 you are incrementing k untill it gets over 20, hence index out of range.
I believe you also have to increment j here.

Note: although it may be fun to code your own sort routines and write a List class (you did it in a rather strange way, but it seems to work:-/ ) it is perhaps better to get a look at the generic List class of .NET, it has a Sort method and you don't have to bother about index values out of range any more!:cool:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.