To keep it short. I made a MergeSort, which should sort an int[]. To test the MergeSort I made an array containing 4 integers in random order. It returns the exact list in the order given to the MergeSort, instead of doing what it should do. I wonder what I did wrong.
Edit: The first two merges return these two lists: 4, 15 && 5, 16, however the last merge doesn't work.
This is the actual code:
public static int[] Merge(int[] list, int left, int mid, int right)
{
int[] returnList = new int[list.Length];
int Index = left;
//last value of left half
int leftEnd = mid;
//both the left side and right side contain atleast one value
while (leftEnd >= left && right >= (mid + 1))
{
if (list[left] <= list[(mid + 1)])
{
returnList[Index] = list[left];
left++;
}
else
{
returnList[Index] = list[(mid + 1)];
mid++;
}
Index++;
}
//right side is empty, left contains atleast one value
while (leftEnd >= left)
{
returnList[Index] = list[left];
left++;
Index++;
}
//left side is empty, right contains atleast one value
while (right >= (mid + 1))
{
returnList[Index] = list[(mid + 1)];
mid++;
Index++;
}
return returnList;
}
public static int[] Split(int[] list, int left, int right)
{
if (right > left)
{
int mid = (right + left) / 2;
Split(list, left, mid); //first half
Split(list, (mid + 1), right); //second half
list = Merge(list, left, (mid + 1), right);
}
return list;
}
My Main:
int[] intArray = new int[] { 12, 4, 16, 5 };
int[] ReturnTest = MergeSort.Split(intArray, 0, intArray.Length - 1);
foreach (int value in ReturnTest)
{
Console.WriteLine(value);
}
The expected output: 4, 5, 12, 16
The actual output: 12, 4, 16, 5
Related
I know there are lot of resources on QuickSort but I need a fresh pair of eyes to catch a bug. I have written the QuickSort Algorithm, but it works for small array, but with large array it throws invalid result (last 3-4 elements didn't sort at all).
Test Case 1:
Input: { 5, 7, 2, 8, 10, 15, 14, 16, 16, 19, 20, 19, 3, 1, 5 }
OutPut: 1,2,3,5,10,14,15,16,16,19,20,19,7,5,8 (WRONG)
Test Case 2:
Input: { 5,1,15,2,8}
Output: 1,2,5,8,15
[TestMethod]
public void QuickSort_test()
{
//input
int[] input = InputArray();
//Algo
int n = input.Length-1;
QuickSort(0,n,input);
//matching result
bool isSorted = IsSorted(input);
Assert.AreEqual(true, isSorted);
}
private void QuickSort(int low, int high, int[] arr)
{
if(low< high)
{
//finding pivot correct index
int idxPivot = Partition(low, high, arr);
//Recursive QuickSort, on elements left on pivot and elements right on pivot
int h = high - idxPivot;
QuickSort(low, idxPivot - 1, arr); //left of pivot
QuickSort(idxPivot + 1, h, arr); //right of pivot
}
}
private int Partition(int low, int high, int[] arr)
{
int pivot = arr[high];
int idxp = low - 1; //smaller than pivot
for (int i = low; i < high; i++) //since element at high is already pivot, no need to run loop till that
{
if(arr[i]< pivot) //current ele < pivot . increment pivotsmallerIndex and swap current element with pivot smaller index
// hence making current element as the smaller element from pivot
{
idxp++;
//swapping
int temp = arr[idxp];
arr[idxp] = arr[i];
arr[i] = temp;
}
}
//idxp contains the last smallest element from the pivot
//so swapping pivot with the next element of idxp
int temp1 = arr[idxp + 1];
arr[idxp+1] = pivot;
arr[high] = temp1;
//return index of pivot, since it is added at idxp+1 now
return idxp + 1;
}
public int[] InputArray()
{
return new int[] { 5, 7, 2, 8, 10, 15, 14, 16, 16, 19, 20, 19, 3, 1, 5 };
//return new int[] { 5,1,15,2,8};
}
I have an array of integers and I want to determine the values for the array integers such that the sum of the fewest values = some specified number.
E.g. int[] array = {1, 2, 4, 8};
specified number = 11;
Clearly the values are 1, 2 and 8 (1+2+8=11), but how do I code it in C#?
A recursion solution -
Go over all numbers from biggest (last position) to smallest (position 0). (Assuming the array is sorted as you wrote although it doesn't really matter.)
In each iteration, check option A with the number in current position against option B without it, and take the one with less items.
static void Main(string[] args)
{
int[] array = { 1, 2, 4, 8 };
int specifiedNumber = 11;
int[] result = FindFewestValues(ref array, array.Length - 1, 0, specifiedNumber);
if (result == null)
Console.WriteLine("No result");
else
{
foreach (int x in result)
Console.Write(x + ", ");
Console.WriteLine();
}
}
static int[] FindFewestValues(ref int[] array, int pos, int tmpSum, int requiredSum)
{
int currentSum = tmpSum + array[pos];
if (currentSum == requiredSum)
return new int[] { array[pos] };
if (pos == 0) // Reached last element
return null;
if (currentSum > requiredSum) // Too big, move to next smaller number
return FindFewestValues(ref array, pos - 1, tmpSum, requiredSum);
// Use current pos:
int[] optionA = FindFewestValues(ref array, pos - 1, currentSum, requiredSum);
// Don't use current pos:
int[] optionB = FindFewestValues(ref array, pos - 1, tmpSum, requiredSum);
if (optionA == null)
{
return optionB;
}
else
{
optionA = optionA.Append(array[pos]).ToArray();
if (optionB == null)
return optionA;
if (optionA.Length < optionB.Length)
return optionA;
else
return optionB;
}
}
i am trying to use an Interpolation Search algorithm to find a value and return it. (Which is what it does currently). I am trying to modify it so it returns a number which i can use to find the closest values to the inputted item if the item which was searched was not found within the array.
public static int InterSearch(double[] array, double data)
{
int size = array.Length;
int lo = 0;
int mid = -1;
int hi = array.Length - 1;
int index = -1;
int count = 0;
while (lo <= hi)
{
mid = (int)(lo + (((double)(hi - lo) / (array[hi] - array[lo])) * (data - array[lo])));
count++;
if (array[mid] == data)
{
index = mid;
break;
}
else
{
if (array[mid] < data)
lo = mid + 1;
else
hi = mid - 1;
}
}
return index;
}
You can use an aggregate that find the closest value.
this is a custom extension method but you get the idea.
public static double GetValueClosestTo(this List<double> values, double closestTo)
{
return values.Aggregate((x, y) => Math.Abs(x - closestTo) < Math.Abs(y - closestTo) ? x : y);
}
Let's say you have the following array {1, 5, 9.2, 6, 17} and you test the following number {6, 15, 5.2}. You will use the following code
var sourceArray = new [] {1, 5, 9.2, 6, 17}.ToList() // for simplicity i use a list
var closestToX = sourceArray.GetValueClosestTo(6); // this return 6
closestToX = sourceArray.GetValueClosestTo(15); // this return 17
closestToX = sourceArray.GetValueClosestTo(5.2); // this return 5
I found this example of a merge sort algorithm online on a tutorial webpage and I have been trying to understand ow the code implements the algorithm. The example i found uses recursion and a temporary array to sort the array of unsorted algorithms.
My query is in the final step of the process. When copying the elements of the temporary array into the original array to sort the array. why does the algorithm decrements the right attribute instead of incrementing the left attribute? when i incremented the left left value the algorithm does not work.
class Assignment1
{
static void Main(string[] args)
{
Console.WriteLine("Size of array:");
int n = Convert.ToInt16(Console.ReadLine());
int[] unsorted = new int[n];
for(int i = 0; i < n; i++)
{
Console.WriteLine("Enter a number:");
unsorted[i] = Convert.ToInt16(Console.ReadLine());
}
Console.WriteLine("--------Sort---------");
Recursion_Sort(unsorted, 0, n - 1);
for (int i = 0; i < n; i++)
{
Console.WriteLine(unsorted[i]);
}
}
static public void Merge(int[] numbers, int left, int mid, int right, int n)
{
int[] tempArray = new int[n];
int i, lEnd, size, pos;
lEnd = mid - 1;
pos = left;
size = (right - left + 1);
while ((left <= lEnd) && (mid <= right))
{
if (numbers[left] <= numbers[mid])
{
tempArray[pos] = numbers[left];
pos++;
left++;
}
else
{
tempArray[pos] = numbers[mid];
pos++;
mid++;
}
}
while (left <= lEnd)
{
tempArray[pos] = numbers[left];
pos++;
left++;
}
while (mid <= right)
{
tempArray[pos] = numbers[mid];
pos++;
mid++;
}
Console.WriteLine(tempArray.Length);
for (i = 0; i < size; i++)
{
numbers[right] = tempArray[right];
right--;
}
}
static public void Recursion_Sort(int[] numbers, int left, int right)
{
int mid;
if (right > left)
{
mid = (right + left) / 2;
Recursion_Sort(numbers, left, mid);
Recursion_Sort(numbers, (mid + 1), right);
// we then merge the sorted sub arrays using the merge method
Merge(numbers, left, (mid + 1), right, numbers.Length);
}
}
}
left value is changing during merge and as you have code block
while (left <= lEnd)
{
//...
left++;
}
left will be finally assigned to lEnd + 1(the condition for ending while loop).
Otherwise right is not changing and is the last index of currently manipulated sequence.
Taking the risk of not answering the question like you want it, I would suggest LINQ. This is not merge sort in particular, but rather a concatenation of two arrays and then sorting.
If your array isn't so big that performance matters, you might want to go for this approach, because it's simple and less code (which is always good).
int[] arr1 = new[] { 1, 2, 3, 7, 8, 10 };
int[] arr2 = new[] { 4, 6, 9, 12, 15 };
int[] merged = arr1.Concat(arr2).OrderBy(n => n).ToArray();
Furthermore, I post this if it is interesting for others.
Attempting to learn from doing an implementation of Quicksort, I cannot find out why it's not sorting properly.
Using this sequence:
6, 7, 12, 5, 9, 8, 65, 3
It returns this:
3, 5, 7, 8, 9, 65, 12, 6
It seems to sort somewhat, but not all. What have I missed?
Here's my code:
static void Main(string[] args)
{
QuickSort qs = new QuickSort();
int[] arr = new int[] { 6, 7, 12, 5, 9, 8, 65, 3 };
foreach (int l in arr)
{
Console.Write(l + ", ");
}
int left = 0;
int right = arr.Count() - 1;
int[] arrr = qs.DoQuickSort(ref arr, left, right);
Console.WriteLine("Sorted List: ");
foreach (int i in arrr)
{
Console.Write(i + " ");
}
Console.ReadLine();
}
public int Partition(int[] array, int left, int right, int PivotIndex)
{
int pivValue = array[PivotIndex];
array = Swap(ref array, PivotIndex, right);
int storeIndex = left;
for (int i = PivotIndex; i < right-1; i++)
{
if (array[i] <= pivValue)
{
array = Swap(ref array, i, storeIndex);
storeIndex = storeIndex + 1;
}
}
array = Swap(ref array, storeIndex, right);
return storeIndex;
}
public int[] Swap(ref int[] arr, int first, int second)
{
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
return arr;
}
public int[] DoQuickSort(ref int[] array, int left, int right)
{
if (right > left)
{
int PivIndex = (left + right) / 2;
int newPivIndex = Partition(array, left, right, PivIndex);
DoQuickSort(ref array, left, newPivIndex - 1);
DoQuickSort(ref array, newPivIndex + 1, right);
}
return array;
}
Are you asking to be handed a fish, or to be taught how to fish?
Learning how to debug your own programs, rather than relying upon other people to do it for you, is a skill that will serve you well in the future.
When faced with this problem, the first thing I would do is mark up the code with comments indicating the semantic purpose of each section of code:
// Choose a pivot halfway along the portion of the list I am searching.
int PivIndex = (left + right) / 2;
// Partition the array so that everything to the left of the pivot
// index is less than or equal to the pivot, and everything to
// the right of the pivot is greater than or equal to the pivot.
int newPivIndex = Partition(array, left, right, PivIndex);
// Recursively sort each half.
DoQuickSort(ref array, left, newPivIndex - 1);
DoQuickSort(ref array, newPivIndex + 1, right);
OK, now, somewhere in here there is a bug. Where? Start listing facts that you believe to be true, and write an assertion for every fact. Write yourself helper methods, like AllLessThan, which verify assertions for you.
// Choose a pivot halfway along the portion of the list I am searching.
int PivIndex = (left + right) / 2;
int pivotValue = array[PivIndex];
// Partition the array so that everything to the left of the pivot
// index is less than or equal to the pivot, and everything to
// the right of the pivot is greater than or equal to the pivot.
int newPivIndex = Partition(array, left, right, PivIndex);
Debug.Assert(array[newPivIndex] == pivotValue);
Debug.Assert(AllLessThanOrEqual(array, left, newPivIndex, pivotValue));
Debug.Assert(AllGreaterThanOrEqual(array, newPivIndex, right, pivotValue));
// Recursively sort each half.
DoQuickSort(ref array, left, newPivIndex - 1);
Debug.Assert(IsSorted(array, left, newPivIndex));
DoQuickSort(ref array, newPivIndex + 1, right);
Debug.Assert(IsSorted(array, left, right));
Now when you run this program again, the moment one of your assertions is violated, you'll get a box that pops up to tell you what the nature of the bug is. Keep doing that, documenting your preconditions and postconditions with assertions until you find the bug.
In your Partition method you have a wrong loop range:
for (int i = PivotIndex; i < right-1; i++)
Should become:
for (int i = left; i < right; i++)
Checkout the related wikipedia article which says:
function partition(array, left, right, pivotIndex)
pivotValue := array[pivotIndex]
swap array[pivotIndex] and array[right] // Move pivot to end
storeIndex := left
for i from left to right - 1 // left ≤ i < right
if array[i] ≤ pivotValue
swap array[i] and array[storeIndex]
storeIndex := storeIndex + 1
swap array[storeIndex] and array[right] // Move pivot to its final place
return storeIndex
Notice: left ≤ i < right
In addition to my comment on the question itself, I wanted to point out that the Swap() and DoQuickSort() methods do not need to return the array (as per my note in the comment which explains that the array is a reference itself). To that end, your code to do the job should look like the following:
public int Partition(int[] array, int left, int right, int pivotIndex)
{
int pivValue = array[pivotIndex];
Swap(array, pivotIndex, right);
int storeIndex = left;
for (int i = left; i < right; i++)
{
if (array[i] <= pivValue)
{
Swap(array, i, storeIndex);
storeIndex = storeIndex + 1;
}
}
Swap(array, storeIndex, right);
return storeIndex;
}
public void Swap(int[] arr, int first, int second)
{
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
public void DoQuickSort(int[] array, int left, int right)
{
if (right > left)
{
int pivIndex = (left + right) / 2;
int newPivIndex = Partition(array, left, right, pivIndex);
DoQuickSort(array, left, newPivIndex - 1);
DoQuickSort(array, newPivIndex + 1, right);
}
}