static void Main(string[] args) { Utilities utils = new Utilities(); MergeSort ms = new MergeSort(); int[] numbers = new int[20]; for (int i = 0; i < numbers.Length; i++) { numbers[i] = utils.random_int(10, 100); } foreach (int n in numbers) { Console.Write(n.ToString() + ", "); } Console.WriteLine(); ms.sort(numbers); foreach (int n in numbers) { Console.Write(n.ToString() + ", "); } Console.Read(); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); //LinearSearch.Do(); //BinarySearch.Do(); //BubbleSort.Do(); //SelectionSort.Do(); //InsertionSort.Do(); //QuickSort.Do(); //recursion.Do(); //shellSort.Do(); MergeSort.Do(); }
static void Main(string[] args) { int[] collection = Console.ReadLine().Split().Select(int.Parse).ToArray(); MergeSort <int> .Sort(collection); StringBuilder sb = new StringBuilder(); foreach (var i in collection) { sb.Append(i + " "); } Console.WriteLine(sb); }
static void Main(string[] args) { int[] unsorted = { 3, 9, 2, 1, 8, 5, 8, 3, 2, 7, 1, 9, 6, 0 }; Print(unsorted); var sorted = MergeSort <int> .SortArray(unsorted); Print(sorted); Print(unsorted); var str = @";lnasd!$#!#%$%^&)*&*(_*?></.,|\][}{(_+(=-+_)+_)(~!@!~@~!$~%$%#$^@^%?<>?MV<ZMCV<ALGA"; Console.WriteLine(str); Console.WriteLine(GenerateValidFileName(str)); }
static void Main(string[] args) { int[] arr = new int[] { 5, 1, 7, 0, 49, 3, 6, 2, 20, 4 }; //BubbleSort.Sort(arr); //InsertionSort.Sort(arr); //SelectionSort.Sort(arr); //QuickSort.Sort(arr); MergeSort.Sort(arr); Console.WriteLine(String.Join(" ", arr)); Console.ReadLine(); }
public static void Test(int length, int minimumVal, int maximumVal) { MergeSort mSort = new MergeSort(); int[] array = Utilities.GenerateRandomizedArray(length, minimumVal, maximumVal); Stopwatch watch = new Stopwatch(); watch.Start(); int[] sortedArray = mSort.Sort(array); watch.Stop(); TimeSpan mergeSortTime = watch.Elapsed; List<int> comparisonList = array.ToList(); watch.Restart(); comparisonList.Sort(); watch.Stop(); TimeSpan dotNETTime = watch.Elapsed; Console.WriteLine(string.Format("Unsorted: {0}{1}Sorted: {2}{1}Elapsed Time: {3}{1}Compare To: {4}{1}", Utilities.FormatPrintArray(array), Environment.NewLine, Utilities.FormatPrintArray(sortedArray), mergeSortTime.ToString(), dotNETTime.ToString())); }
static void Main(string[] args) { // bubble sort var array = new int[] { 1, 4, 3, 7, 44, 34, 6, 11, 36, 43 }; var sorted = BubbleSort.Sort(array); foreach (int item in sorted) { Console.Write(item.ToString()); } // binary search var myArray = new int[] { 1, 2, 4, 6, 10, 14, 15, 19, 20, 34, 36, 38, 40, 42 }; var value = BinarySearch.BSearch(10, myArray); // merge sort int[] mergeSorted = MergeSort.Sort(array); Console.ReadKey(); }
private static void DoMergeSort() { int[] array = new int[] { 5, 3, 8, 9, 1, 7, 0, 2, 6, 4 }; MergeSort mSort = new MergeSort(); mSort.Sort(array); }
static void Main(string[] args) { int[] data = new int[] { 5, 2, 1, 4, 3, 5, 0, 9, 5, 4, 3, 2, 1, 0 }; Console.WriteLine("Heap Sort"); int[] sorted1 = new HeapSort(data, data.Length).Sort(); HeapSort.PrintArray(sorted1, sorted1.Length); Console.WriteLine("Quick Sort"); int[] sorted2 = new QuickSort(data, data.Length).Sort(); QuickSort.PrintArray(sorted1, sorted1.Length); Console.WriteLine("Merge Sort"); int[] sorted3 = new MergeSort(data, data.Length).Sort(); MergeSort.PrintArray(sorted1, sorted1.Length); Console.WriteLine("Graph - BFS"); Graph g = new Graph(4); g.AddEdge(0, 1); g.AddEdge(0, 2); g.AddEdge(1, 2); g.AddEdge(2, 0); g.AddEdge(2, 3); g.AddEdge(3, 3); g.BFS(2, (i) => Console.Write("{0} ", i)); Console.WriteLine(string.Empty); Console.WriteLine("Graph - DFS"); g.DFS(2, (i) => Console.Write("{0} ", i)); Console.WriteLine(string.Empty); Graph g1 = new Graph(9); List <List <int> > g2 = new List <List <int> >() { new List <int>() { 0, 4, 0, 0, 0, 0, 0, 8, 0 }, new List <int>() { 4, 0, 8, 0, 0, 0, 0, 11, 0 }, new List <int>() { 0, 8, 0, 7, 0, 4, 0, 0, 2 }, new List <int>() { 0, 0, 7, 0, 9, 14, 0, 0, 0 }, new List <int>() { 0, 0, 0, 9, 0, 10, 0, 0, 0 }, new List <int>() { 0, 0, 4, 14, 10, 0, 2, 0, 0 }, new List <int>() { 0, 0, 0, 0, 0, 2, 0, 1, 6 }, new List <int>() { 8, 11, 0, 0, 0, 0, 1, 0, 7 }, new List <int>() { 0, 0, 2, 0, 0, 0, 6, 7, 0 } }; for (int i = 0; i < g2.Count; i++) { for (int j = 0; j < g2[i].Count; j++) { if (g2[i][j] > 0) { g1.AddEdge(i, j, g2[i][j]); } } } g1.PrintDistance(g1.Dijakstra(0), 0); Console.WriteLine(string.Empty); Console.WriteLine("Binary Search"); int[] arr = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; foreach (int i in new int[] { -5, 1, 5, 8, 0, 10, 11 }) { Console.WriteLine("Position of {0} = {1}", i, Search.BinarySearch(arr, i)); } Console.WriteLine("Binary Search Next greatest - 1"); int[] arr2 = new int[] { 100, 50, 40, 20, 10 }; foreach (int i in new int[] { 5, 25, 50, 120 }) { Console.WriteLine("Position of {0} = {1}", i, Search.BinarySearchNextGreatest(arr2, i)); } Console.WriteLine("Binary Search Next greatest - 2"); int[] arr3 = new int[] { 100, 90, 80, 75, 60 }; foreach (int i in new int[] { 50, 65, 77, 90, 102 }) { Console.WriteLine("Position of {0} = {1}", i, Search.BinarySearchNextGreatest(arr3, i)); } Console.ReadKey(); }
static void Main(string[] args) { Console.WriteLine("Algorithms!"); #region Merge Sorting int[] array = { 11, 13, 7, 12, 16, 9, 24, 5, 10, 3 }; MergeSort.MergeSortWithTwoArray(); MergeSort.RecursiveMergeSort(array, 0, array.Length - 1); Console.WriteLine($"Recursive Merge Sort : [{string.Join(",", array)}]"); #endregion #region Binary Search Console.WriteLine(); Console.WriteLine("Binary Search"); int[] binarySearchArray = new int[] { 2, 8, 12, 22, 35, 88, 100 }; Console.WriteLine($"The index of given element in a Binary Search is : {BinarySearch.BinarySearch.BinarySearchAlgorithm(binarySearchArray, binarySearchArray.Length, 88)}"); #endregion #region Bubble Sort Console.WriteLine(); Console.WriteLine("Bubble Sort!"); int[] bubbleSortArray = new int[] { 10, 8, 12, 22, 100, 88, 101, 4 }; BubbleSort.BubbleSort.BubbleSortAlgorithm(bubbleSortArray, bubbleSortArray.Length); #endregion #region Count Sort Console.WriteLine(); Console.WriteLine("Count Sort!"); CountSort.CountSort.CountSortAlgoritm(); #endregion #region Fibonacci Series Console.WriteLine(); Console.WriteLine("Fibonacci Series!"); FibonacciSeries.FibonacciSeries.FibonacciSeriesAlgorithm(); #endregion #region Factorial Console.WriteLine(); Console.WriteLine("Factorial !!"); Factorial.Factorial.FactorialMethod(5); #endregion #region Recursion Console.WriteLine("Recursion Ascending & Descending Sorting"); Recursion.Recursion.RecursionSort(3); #endregion #region Reverse a String Console.WriteLine(); Console.WriteLine("Reverse a String !!"); Basics.ReverseAString.ReverseString("rajeesh"); #endregion #region Missing number problem in an array MissingNumberProblem.FindMissingNumberInAnArray(); #endregion #region MinAndMax Array Problems Console.WriteLine("Minimum and Maximum Problems in an Arrays!"); int[] arrayElement = { 10, 88, 1, 55, 48 }; MinAndMax.MinAndMaxSumInAnArray(arrayElement); int[] arrMinAndMax = { 3, 10, 2, 5, 7, 10 }; MinAndMax.FindMaxValueCountInAnArray(arrMinAndMax); #endregion Console.WriteLine(); Console.WriteLine("Multidimensional Arrays!"); #region Multidimensional Array TwoDimensionalArray.ReplaceDiagonalWithStar(); Console.WriteLine("Matrix Rotation"); TwoDimensionalArray.NinetyDegreeMatrixRotation(); #endregion Console.ReadLine(); }
private static void MergeSort() { var sort = new MergeSort(); sort.Run(); }
static void Main(string[] args) { Console.WriteLine("Warmup"); var stopwatch = Stopwatch.StartNew(); long seed = Environment.TickCount; // Prevents the JIT Compiler // from optimizing Fkt calls away long result = 0; int count = 100000000; while (stopwatch.ElapsedMilliseconds < 1200) // A Warmup of 1000-1500 mS // stabilizes the CPU cache and pipeline. { result = TestFunction(seed, count); // Warmup } stopwatch.Stop(); int arraySize = 1000000; var runCount = 2; stopwatch.Reset(); stopwatch.Start(); var stringArray = GetStringArray(arraySize); stopwatch.Stop(); Console.WriteLine($"Initializing a {arraySize} elements string array: {stopwatch.ElapsedMilliseconds} ms"); stopwatch.Reset(); stopwatch.Start(); var intArray = GetIntArray(arraySize); stopwatch.Stop(); Console.WriteLine($"Initializing a {arraySize} elements Integer array: {stopwatch.ElapsedMilliseconds} ms"); stopwatch.Reset(); stopwatch.Start(); var stringList = GetStringArrayList(arraySize); stopwatch.Stop(); Console.WriteLine($"Initializing a {arraySize} elements string List: {stopwatch.ElapsedMilliseconds} ms"); var currentCount = 0; Console.WriteLine("-------------------------------------------------------------"); Console.WriteLine("-------------- Integer Bubble Sort ---------------------"); Console.WriteLine("-------------------------------------------------------------"); while (currentCount < runCount) { stopwatch.Reset(); stopwatch.Start(); BubbleSort.Sort(intArray = GetIntArray(arraySize)); stopwatch.Stop(); Console.WriteLine($"Bubble Sorting an Integer array of {arraySize} elements {stopwatch.ElapsedMilliseconds} ms"); //PrintTop10(intArray); currentCount++; } currentCount = 0; Console.WriteLine("-------------------------------------------------------------"); Console.WriteLine("------------ Integer Bubble Sort (Parallel) ----------------"); Console.WriteLine("-------------------------------------------------------------"); while (currentCount < runCount) { stopwatch.Reset(); stopwatch.Start(); BubbleSort.sortParallel(GetIntArray(arraySize)); stopwatch.Stop(); Console.WriteLine($"Bubble Sorting an Integer array of {arraySize} elements {stopwatch.ElapsedMilliseconds} ms"); //PrintTop10(intArray); currentCount++; } currentCount = 0; Console.WriteLine("-------------------------------------------------------------"); Console.WriteLine("-------------- Integer Merge Sort ---------------------"); Console.WriteLine("-------------------------------------------------------------"); while (currentCount < runCount) { stopwatch.Reset(); stopwatch.Start(); MergeSort.Sort(GetIntArray(arraySize)); stopwatch.Stop(); Console.WriteLine($"Merge Sorting an Integer array of {arraySize} elements {stopwatch.ElapsedMilliseconds} ms"); //PrintTop10(intArray); currentCount++; } //currentCount = 0; //Console.WriteLine("-------------------------------------------------------------"); //Console.WriteLine("-------------- String Bubble Sort ---------------------"); //Console.WriteLine("-------------------------------------------------------------"); //while (currentCount < runCount) //{ // stopwatch.Reset(); // stopwatch.Start(); // BubbleSort.Sort(GetStringArray(arraySize)); // stopwatch.Stop(); // Console.WriteLine($"Bubble Sorting a string array of {arraySize} elements {stopwatch.ElapsedMilliseconds} ms"); // //PrintTop10(stringArray); // currentCount++; //} currentCount = 0; Console.WriteLine("-------------------------------------------------------------"); Console.WriteLine("-------------- String Merge Sort ---------------------"); Console.WriteLine("-------------------------------------------------------------"); while (currentCount < runCount) { stopwatch.Reset(); stopwatch.Start(); MergeSort.Sort(GetStringArray(arraySize)); stopwatch.Stop(); Console.WriteLine($"Bubble Sorting a string array of {arraySize} elements {stopwatch.ElapsedMilliseconds} ms"); //PrintTop10(stringArray); currentCount++; } currentCount = 0; Console.WriteLine("-------------------------------------------------------------"); Console.WriteLine("-------------- String List Merge Sort ---------------------"); Console.WriteLine("-------------------------------------------------------------"); while (currentCount < runCount) { stopwatch.Reset(); stopwatch.Start(); MergeSort.Sort(GetStringArrayList(arraySize)); stopwatch.Stop(); Console.WriteLine($"Bubble Sorting a string array of {arraySize} elements {stopwatch.ElapsedMilliseconds} ms"); //PrintTop10(stringArray); currentCount++; } Console.Read(); }
/// <summary> /// Defines the entry point of the application. /// </summary> /// <param name="args">The arguments.</param> public static void Main(string[] args) { try { string condition = "null"; ////this loop is used for running the code countineously util the condition is false do { Console.WriteLine("enter 1 for anagram"); Console.WriteLine("enter 2 for primenumbers"); Console.WriteLine("enter 3 for day Of Week"); Console.WriteLine("enter 4 for temperature conversion"); Console.WriteLine("enter 5 for anagram and palindrome of prime numbers"); Console.WriteLine("enter 6 for Vending Machine"); Console.WriteLine("enter 7 for monthly payments"); Console.WriteLine("enter 8 for converting decimal to binary"); Console.WriteLine("enter 9 for Swap nibbles and find the new number"); Console.WriteLine("enter 10 for Search And Sort"); Console.WriteLine("enter 11 for the square root of a nonnegative number"); Console.WriteLine("enter 12 for searching the string in a file"); Console.WriteLine("enter 13 for insertion sort through file"); Console.WriteLine("enter 14 for bubble sort of integers through file"); Console.WriteLine("enter 15 for find your number"); Console.WriteLine("enter 16 for mergesort"); int i = Convert.ToInt32(Console.ReadLine()); switch (i) { case 1: Anagram anagram = new Anagram(); anagram.ToFindAnagram(); break; case 2: PrimeNumbers primeNumbers = new PrimeNumbers(); primeNumbers.FindPrimeNumbers(); break; case 3: WeekOfADay weekOfADay = new WeekOfADay(); weekOfADay.FindingWeekOfADay(); break; case 4: ConversionOfTemperature conversionOfTemperature = new ConversionOfTemperature(); conversionOfTemperature.ConvertingTemperatures(); break; case 5: PalindromeAndAnagramOfPrimeNumbers palindromeAndAnagramOfPrime = new PalindromeAndAnagramOfPrimeNumbers(); palindromeAndAnagramOfPrime.PalindromeOfPrimeNumbers(); palindromeAndAnagramOfPrime.AnagramOfPrimeNumbers(); break; case 6: VendingMachine vendingMachine = new VendingMachine(); vendingMachine.CountingChange(); break; case 7: MonthlyPayments monthlyPayments = new MonthlyPayments(); monthlyPayments.CalculationOfMonthlyPayments(); break; case 8: DecimalToBinary decimalToBinary = new DecimalToBinary(); decimalToBinary.Conversion(); break; case 9: Binary binary = new Binary(); binary.NewNumber(); break; case 10: SearchAndSort searchAndSort = new SearchAndSort(); searchAndSort.SearchAndSortedList(); break; case 11: SqrtOfNonNegativeNumber sqrtOfNonNegative = new SqrtOfNonNegativeNumber(); sqrtOfNonNegative.Sqrt(); break; case 12: BinarySearchThroughFile searchThroughFile = new BinarySearchThroughFile(); searchThroughFile.BinarySearchOfAStringInFile(); break; case 13: InsertionSortThroughFile insertionSortThroughFile = new InsertionSortThroughFile(); insertionSortThroughFile.SortingStringsInFile(); break; case 14: BubbleSortThroughFile bubbleSortThroughFile = new BubbleSortThroughFile(); bubbleSortThroughFile.SortingIntigersInFile(); break; case 15: FindYourNumber findYourNumber = new FindYourNumber(); findYourNumber.GuessNumber(); break; case 16: MergeSort mergeSort = new MergeSort(); Console.WriteLine("enter size of array"); int size = Convert.ToInt32(Console.ReadLine()); string[] words = new string[size]; Console.WriteLine("enter strings in to array"); for (int k = 0; k < size; k++) { words[k] = Console.ReadLine(); } mergeSort.Sort(words); Console.WriteLine("sorted array"); foreach (string sortedArray in words) { Console.WriteLine(sortedArray); } break; } Console.WriteLine("enter yes to execute remaining programs or enter no to stop execution"); condition = Console.ReadLine(); }while (condition == "yes"); } catch (Exception e) { Console.WriteLine(e.Message); } }
static void Main(string[] args) { int n = Convert.ToInt32(Console.ReadLine()); int[] A = new int[n]; Random random = new Random(); for (int i = 0; i < n; ++i) { A[i] = random.Next(0, 100); Console.WriteLine(A[i] + " "); } string a = Console.ReadLine(); int size = a.Length; char[] b = new char[size]; Change(a, b); for (int i = 0; i < b.Length; ++i) { switch (b[i]) { case '1': Console.WriteLine("Insertion Sort "); int[] B = new int[A.Length]; for (int k = 0; k < B.Length; ++k) { B[k] = A[k]; } InsertionSort.insertionSort(B); InsertionSort.PrintArr(B); break; case '2': Console.WriteLine("Merge Sort"); int[] C = new int[A.Length]; for (int k = 0; k < C.Length; ++k) { C[k] = A[k]; } MergeSort.mergeSort(C, 0, C.Length - 1); MergeSort.PrintArr(C); break; case '3': Console.WriteLine("Bubble Sort"); int[] D = new int[A.Length]; for (int k = 0; k < D.Length; ++k) { D[k] = A[k]; } BubbleSort.BBSort(D); BubbleSort.PrintArr(D); break; case '4': Console.WriteLine("Quick Sort"); int[] E = new int[A.Length]; for (int k = 0; k < E.Length; ++k) { E[k] = A[k]; } QuickSort.quicksort(E, 0, E.Length - 1); QuickSort.PrintArr(E); break; } } Console.ReadKey(); }