Tuesday, October 28
Merge Sort MultiThreading in C#; multithreaded
int[] MergeSort(int[] input, int start, int end) { if (end - start < 2) { int[] result = new int[1]; result[0] = input[start]; return (result); } Task<int[]> Tleft = Task<int[]>.Factory.StartNew(() => { return MergeSort(input, start, (start + end) / 2); }); Task<int[]> Tright = Task<int[]>.Factory.StartNew(() => { return MergeSort(input, (start + end) / 2, end); }); //Task<int[]>.WaitAll(new Task<int[]>[]{Tleft,Tright}); int[] left = Tleft.Result; int[] right = Tright.Result; int[] Result = Task<int[]>.Factory.StartNew(() => { return Merge(left, right); }).Result; Console.WriteLine(getText(Result)); return Result; } int[] Merge(int[] left, int[] right) { int leftSize = left.Length, rightSize = right.Length; int[] result = new int[leftSize + rightSize]; int leftIndex = 0, rightIndex = 0, resultIndex = 0; while (rightIndex < rightSize && leftIndex < leftSize) { result[resultIndex++] = left[leftIndex] < right[rightIndex] ? left[leftIndex++] : right[rightIndex++]; } while (rightIndex < rightSize) result[resultIndex++] = right[rightIndex++]; while (leftIndex < leftSize) result[resultIndex++] = left[leftIndex++]; return result; }
Monday, October 27
Merge Sort
Please find the source code for merge sort algorithm. I have made this implementation on my own after taking this class https://class.coursera.org/algo-006/lecture/2
I will appreciate any comment that you have.
I will appreciate any comment that you have.
#include <iostream> using namespace std; int* Merge(int*, int*, const int, const int); int* MergeSort(int* input, int start, int end, int& resultSize) { if (end - start < 2) { int * result = new int[1]; result[0] = input[start]; resultSize = 1; return (result); } int leftSize = 0, rightSize = 0; int *left = MergeSort(input, start, (start + end) / 2, leftSize); int *right = MergeSort(input, (start + end) / 2, end, rightSize); int* result = Merge(left, right, leftSize, rightSize); resultSize = leftSize + rightSize; delete[] left; delete[] right; left = right = NULL; //to avoid dangling pointer return result; } int* Merge(int* left, int* right, const int leftSize, const int rightSize) { int resultSize = leftSize + rightSize; int * result = new int[resultSize]; int leftIndex = 0, rightIndex = 0, resultIndex = 0; while (rightIndex<rightSize && leftIndex<leftSize) { result[resultIndex++] = left[leftIndex] < right[rightIndex] ? left[leftIndex++] : right[rightIndex++]; } while (rightIndex < rightSize) result[resultIndex++] = right[rightIndex++]; while (leftIndex < leftSize) result[resultIndex++] = left[leftIndex++]; return result; } void main() { int* input = new int[20]{5, 34, 5, 6, 4, 56, 4, 3, 3, 5, 6, 4, 3, 5, 6, 87, 54, 3, 5, 76}; int resultSize = 0; int* result = MergeSort(input, 0, 20, resultSize); for (size_t i = 0; i < resultSize; i++) { cout << endl << result[i]; } delete[] input; delete[] result; }
Sunday, October 26
Relaunching with a course on Design and Analysis of Algorithms from Stanford via Coursera
I will be relaunching this blog in sync with my quarter goal. I am working as an Assoc Software Engineer with Bentley Systems, which is a leading company in AEC infrastructure support. I love being there with all the people. As a part of my career growth i have participated in a MOOC course on Algorithms from Stanford via coursera. I plan to share two posts per week on what i have learned from that course.
Have a great weekend and keep following ;)
PS. Please find the link to this course --> https://www.coursera.org/course/algo
Regards