public static void Main(string[] args) { string[] file = File.ReadAllLines("/Users/redahanb/GitHub/Coursera_Algorithms/1 MergeSort/IntegerArray.txt"); int[] intArray = new int[file.Length]; for (int i = 0; i < file.Length; i++) { //Console.WriteLine(file[i]); intArray[i] = int.Parse(file[i]); } // testing merging int[] output = mergeSort(intArray); for (int i = 0; i < 10; i++) { Console.WriteLine(output[i]); } Console.WriteLine("Inversions counted: " + Inversions.printInv()); }
private String m_type = ""; // joystick OR xboxpad #endregion Fields #region Constructors // ctor public OptionsInvert( Inversions item ) { m_option = item.ToString(); }
String m_option = ""; // the option name (level where it applies) // ctor public OptionsInvert(Inversions item) { m_option = item.ToString(); }
public static void Run2() { var fileName = @"Data\input_1000_100.txt"; var fileInfo = new FileInfo(fileName); var fileStream = fileInfo.OpenRead(); var textReader = new StreamReader(fileStream); var isMatrixCreated = false; var isMatrixFilled = false; int[][] matrix = null; Inversions inversion = null; int users = 0, movies = 0; int userIndex = 0; while (!textReader.EndOfStream) { var row = textReader.ReadLine(); if (!isMatrixCreated) { var splitRow = row.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); Console.WriteLine(row + " "); users = Convert.ToInt32(splitRow[0]); movies = Convert.ToInt32(splitRow[1]); matrix = new int[users][]; for (var i = 0; i < users; ++i) { matrix[i] = new int[movies]; } isMatrixCreated = true; continue; } if (!isMatrixFilled) { var splitRow = row.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); Console.Write("{0} - ", userIndex + 1); for (var i = 0; i < movies; ++i) { matrix[userIndex][i] = Convert.ToInt32(splitRow[i + 1]); Console.Write(matrix[userIndex][i] + " "); } Console.WriteLine(); ++userIndex; } } inversion = new Inversions(matrix); //Пользователь 618 та 1 var result1 = inversion.Inversion(618, 1); Console.WriteLine("Расcчитаем инверсии для пользователя {0:###} c пользователем {1:###} Result:{2:###}", 618, 1, result1); //Пользователь 951 та 178. var result2 = inversion.Inversion(951, 178); Console.WriteLine("Расcчитаем инверсии для пользователя {0:###} c пользователем {1:###} Result:{2}", 951, 178, result2); }
public static void Test() { //var fileName = @"Data\data_examples_02\test_10_5.txt"; var fileName = @"Data\data_examples_02\test_100_50.txt"; //var fileName = @"Data\data_examples_02\test_5_10.txt"; //var fileName = @"Data\data_examples_02\test_5_5.txt"; //var fileName = @"Data\data_examples_02\test_50_100.txt"; var fileInfo = new FileInfo(fileName); var fileStream = fileInfo.OpenRead(); var textReader = new StreamReader(fileStream); var isMatrixCreated = false; var isMatrixFilled = false; int[][] matrix = null; Inversions inversion = null; int users = 0, movies = 0; int userIndex = 0; int userCompareIndex = 0; while (!textReader.EndOfStream) { var row = textReader.ReadLine(); if (string.IsNullOrEmpty(row)) { Console.WriteLine(); } if (row.StartsWith("#")) { Console.WriteLine(row); if (row.StartsWith("# Для користувача №")) { var indexStart = 19; var indexEnd = 19; while (Char.IsDigit(row[indexEnd])) { ++indexEnd; } userCompareIndex = Convert.ToInt32(row.Substring(indexStart, indexEnd - indexStart)); } continue; } if (string.IsNullOrEmpty(row) && !isMatrixCreated) { Console.WriteLine("------------------"); continue; } if (string.IsNullOrEmpty(row) && isMatrixCreated && !isMatrixFilled) { Console.WriteLine("------------------"); isMatrixFilled = true; inversion = new Inversions(matrix); continue; } if (!string.IsNullOrEmpty(row) && isMatrixCreated && isMatrixFilled) { var splitRow = row.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); var user2Index = Convert.ToInt32(splitRow[0]); var user2Result = Convert.ToInt32(splitRow[1]); var result = inversion.Inversion(userCompareIndex, user2Index); Console.Write("Расcчитаем инверсии для пользователя {0:###} c пользователем {1:###} Ответ:{2:###}", userCompareIndex, user2Index, user2Result); Console.Write(" Result:{0:###} - ", result); Console.ForegroundColor = result == user2Result ? ConsoleColor.Green : ConsoleColor.Red; Console.WriteLine(result == user2Result ? "Passed" : "Failed"); Console.ResetColor(); } if (!isMatrixCreated) { var splitRow = row.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); Console.WriteLine(row + " "); users = Convert.ToInt32(splitRow[0]); movies = Convert.ToInt32(splitRow[1]); matrix = new int[users][]; for (var i = 0; i < users; ++i) { matrix[i] = new int[movies]; } isMatrixCreated = true; continue; } if (!isMatrixFilled) { var splitRow = row.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); for (var i = 0; i < movies; ++i) { matrix[userIndex][i] = Convert.ToInt32(splitRow[i + 1]); Console.Write(matrix[userIndex][i] + " "); } Console.WriteLine(); ++userIndex; } } }
public static int[] mergeSort(int[] entry) { Console.WriteLine("Calculating..."); if (entry.Length == 1) { return(entry); } else { // split array in half int[] arrayA = new int[entry.Length - entry.Length / 2]; // in case the length is odd in number int[] arrayB = new int[entry.Length / 2]; Array.Copy(entry, 0, arrayA, 0, arrayA.Length); Array.Copy(entry, arrayA.Length, arrayB, 0, arrayB.Length); // merge sort the half-arrays (recursion until array is length 1, then merge sort them so inversions are counted while merging int[] arrayX = mergeSort(arrayA); int[] arrayY = mergeSort(arrayB); int[] returnArray = new int[entry.Length]; int countA = 0, countB = 0; // merge the sliced arrays back together, sorted into the return array for (int n = 0; n < returnArray.Length; n++) { if (countA == arrayX.Length) { // arrayX is exhausted - end case returnArray[n] = arrayY[countB]; countB++; } else if (countB == arrayY.Length) { // arrayY is exhausted - end case returnArray[n] = arrayX[countA]; countA++; } else if (arrayX[countA] < arrayY[countB]) // compare first terms or each array, keep tally of which index is being examined { returnArray[n] = arrayX[countA]; if (countA < arrayX.Length) { countA++; } } else if (arrayX[countA] > arrayY[countB]) { returnArray[n] = arrayY[countB]; // Inversion is noted here int diff = arrayX.Length - countA; // depends upon the number of elements left in the first array for (int h = 0; h < diff; h++) { Inversions.countInv(); } if (countB < arrayY.Length) { countB++; } } } return(returnArray); } } // end mergesort