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();
}
}
}
AnkitKumar
4
Newbie Poster
Recommended Answers
Jump to PostPlease wrap your code between code tages [code] your code here... [/code]
Mention the line of code it breaks on
Jump to PostHow 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?
Jump to PostIs 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 …
All 10 Replies
Ramy Mahrous
401
Postaholic
Featured Poster
sknake
1,622
Senior Poster
Featured Poster
AnkitKumar
4
Newbie Poster
ddanbe
2,724
Professional Procrastinator
Featured Poster
AnkitKumar
4
Newbie Poster
AnkitKumar
4
Newbie Poster
kvprajapati
1,826
Posting Genius
Team Colleague
Ramy Mahrous
401
Postaholic
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
ddanbe
2,724
Professional Procrastinator
Featured Poster
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.