Skip to main content

Posts

Showing posts with the label algorithm on merge sort

Sample C Program To Implement Merge Sort Using Divide & Conquer Strategy.

ALGORITHM: STEP 1: Start the program. STEP 2: Read the value of n. STEP 3: Set a for loop to read the elements of array. for (i = 0; i < n; i++) STEP 4: Call the function split ( a, 0, n – 1 ) STEP 5: Print the sorted array a. STEP 6: Stop the program. FUNCTION MERGE_SORT (int *a, int low, int high) STEP 1: Declare the local variable. STEP 2: If low is less than high then assign the mean value of low and high to mid. mid = ( low + high ) / 2 STEP 3: Call the function merge_sort ( a, low, mid ). STEP 4: Call the another function merge_sort ( a, mid + 1, high ). STEP 5: Call the function combine ( a, low, mid, high ). FUNCTION SPLIT(int *c, int first, int last) STEP 1: Declare the local variables. STEP 2: Set the while loop till the condition i <= mid && j <= high is failed. STEP 3: Check whether a[i] < a[j] STEP 4: If so the assign the value of a[j] to temp[k] and increment j and k temp[k] = a[i] j++ k++ STEP 5: Else as...