Task1.
Bubble Code
using System;
namespace SortingExample
{
class Program
{
static void Main(string[] args)
{
int[] number = { 89, 76, 45, 92, 67, 12, 99
};
bool flag = true;
int temp;
int numLength = number.Length;
//sorting an array
for (int i = 1; (i <= (numLength - 1)) && flag; i++)
{
flag = false;
for (int j = 0; j < (numLength - 1); j++)
{
if (number[j + 1] > number[j])
{
temp = number[j];
number[j] = number[j + 1];
number[j + 1] = temp;
flag = true;
}
}
}
//Sorted array
foreach (int num in number)
{
Console.Write("\t {0}",num);
}
Console.Read();
}
}
}
Insertion with same data
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InsertionSort
{
class Program
{
static int InsertionSorting()
{
Console.Write("\nProgram for sorting a numeric array using Insertion Sorting");
int[] numarray = { 89, 76, 45, 92, 67, 12, 99 };
int max = numarray.Length;
Console.Write("Input int array : ");
for (int k = 0; k < max; k++)
Console.Write(numarray[k] + " ");
Console.Write("\n");
for (int i = 1; i < max; i++)
{
int j = i;
while (j > 0)
{
if (numarray[j - 1] > numarray[j])
{
int temp = numarray[j - 1];
numarray[j - 1] = numarray[j];
numarray[j] = temp;
j--;
}
else
break;
}
Console.Write("Iteration " + i.ToString() + ": ");
for (int k = 0; k < max; k++)
Console.Write(numarray[k] + " ");
Console.Write("\n");
//Console.Write("/*** " + (i + 1).ToString() + " numbers from the begining of the array are input and they are sorted ***/\n");
}
Console.Write("\n\nThe numbers in ascending orders are given below:\n\n");
for (int i = 0; i < max; i++)
{
Console.Write("Sorted [" + (i + 1).ToString() + "] element: ");
Console.Write(numarray[i]);
Console.Write("\n");
}
return 0;
}
static void Main(string[] args)
{
InsertionSorting();
Console.ReadLine();
}
}
}
Insertion taking data
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace InsertionSort
{
class Program
{
static int InsertionSorting()
{
Console.Write("\nProgram for sorting a numeric array using Insertion Sorting");
Console.Write("\n\nEnter number of elements: ");
int max = Convert.ToInt32(Console.ReadLine());
int[] numarray = new int[max];
for (int i = 0; i < max; i++)
{
Console.Write("\nEnter [" + (i + 1).ToString() + "] element: ");
numarray[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Input int array : ");
for (int k = 0; k < max; k++)
Console.Write(numarray[k] + " ");
Console.Write("\n");
for (int i = 1; i < max; i++)
{
int j = i;
while (j > 0)
{
if (numarray[j - 1] > numarray[j])
{
int temp = numarray[j - 1];
numarray[j - 1] = numarray[j];
numarray[j] = temp;
j--;
}
else
break;
}
Console.Write("Iteration " + i.ToString() + ": ");
for (int k = 0; k < max; k++)
Console.Write(numarray[k] + " ");
Console.Write("\n");
//Console.Write("/*** " + (i + 1).ToString() + " numbers from the begining of the array are input and they are sorted ***/\n");
}
Console.Write("\n\nThe numbers in ascending orders are given below:\n\n");
for (int i = 0; i < max; i++)
{
Console.Write("Sorted [" + (i + 1).ToString() + "] element: ");
Console.Write(numarray[i]);
Console.Write("\n");
}
return 0;
}
static void Main(string[] args)
{
InsertionSorting();
Console.ReadLine();
}
}
}
Материалы на данной страницы взяты из открытых источников либо размещены пользователем в соответствии с договором-офертой сайта. Вы можете сообщить о нарушении.