0% found this document useful (0 votes)
2 views5 pages

Data Structure

The document outlines algorithms for four sorting methods: Merge Sort, Insertion Sort, Radix Sort, Quick Sort, and Selection Sort. Each algorithm is broken down into steps, detailing the initialization of arrays, sorting processes, and functions for printing sorted arrays. The document serves as a guide for implementing these sorting algorithms in a programming context.

Uploaded by

gsuriya2431
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Data Structure

The document outlines algorithms for four sorting methods: Merge Sort, Insertion Sort, Radix Sort, Quick Sort, and Selection Sort. Each algorithm is broken down into steps, detailing the initialization of arrays, sorting processes, and functions for printing sorted arrays. The document serves as a guide for implementing these sorting algorithms in a programming context.

Uploaded by

gsuriya2431
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ALGORITHM FOR MERGE SORT✓

STEP 1: Declare and initialize an integer array 'arr' with values


{6, 5, 12, 10, 9, 1}.

STEP 2: Calculate the size of the array 'size'.

STEP 3: Call 'mergeSort' with 'arr', starting index 0, and


ending index 'size-1'.

STEP 4: Inside 'mergeSort', if l is less than 'r', calculate the


middle index 'm'.

STEP 5: Recursively call 'mergeSort' for the left and right


subarrays.

STEP 6: Merge the sorted left and right subarrays using the
'merge' function.

STEP 7: Inside 'merge', create subarrays 'L' and 'M', and


merge them into the main array.

STEP 8: In the main function, print the sorted array using the
printArray function.

STEP 9: Inside printArray, iterate over the array and print each
element.

STEP 10: Print a newline character after printing all elements.

STEP 11: End the program.


ALGORITHM FOR INSERTION SORT ✓

STEP 1: Declare and initialize an array 'arr' with values {12,


11, 13, 5, 6}.

STEP 2: Calculate the size of the array 'n'.

STEP 3: Call 'insertionSort' with 'arr' and 'n'.

STEP 4: Inside insertionSort, iterate over the array.

STEP 5: For each element, find its correct position in the


sorted part of the array.

STEP 6: Move larger elements to make space for the current


element.

STEP 7: Insert the current element at its correct position.

STEP 8: Print the sorted array using the 'printArray' function.

STEP 9: Inside 'printArray', iterate over the array and print


each element.

STEP 10: Return 0 from the main function.

ALGORITHM FOR RADIX SORT✓

STEP 1: getMax Function:

•Find the maximum element in an


array.
STEP 2: countingSort Function:

• Sort elements based on the current


digit place.
• Use counting sort as a subroutine.

STEP 3: radixsort Function:

•Find the maximum element.


Perform counting sort for each digit
place.

STEP 4: `printArray` Function:

• Print elements of an array.



STEP 5: Main Function (`main`):

• Declare an array.
• call 'radix sort'
• Print the sorted array.

STEP 6: Execution:

• Initialize array in `main`.


• Call 'radixsort`.
• Print sorted array.

ALGORITHM FOR QUICK SORT ✓

STEP 1: Swap Function:

•Swap values pointed to by 'a' and


'b'.
STEP 2: Partition Function:

•Choose the rightmost element as


`pivot`.
•Initialize 'i' to '(low 1)`.
•Iterate from 'low'to 'high -1':
• If element <= `pivot`,
increment i' and swap
elements at 'i' and `j`.
•Swap element at (i + 1)` with
`pivot`, return `(i + 1)`.

STEP 3: QuickSort Function

•If low<high':
•Find partition using `partition`
function.
• Recursively call `quickSort` on
left and right partitions.

STEP 4: PrintArray Function:

•Print elements of the array


separated by two spaces.

STEP 5: Main Function:

• Initialize array `data`.


• Print unsorted array using
`printArray`.
• Call `quickSort to sort the array.
• Print sorted array using
`printArray

ALGORITHM FOR SELECTION SORT ✓

STEP 1: Define swap(a, b) function:

•Swap values at pointers a and b.

STEP 2: Define selectionSort(array, size) function:

•Iterate through array elements


(outer loop).
• Find the index of the minimum
element in unsorted portion (inner
loop).
•Swap the minimum element with
the first unsorted element.
STEP 3: Define printArray(array, size) function:

•Print each element of the array.

STEP 4: Define main() function:

• Initialize an array, data.


•Call selectionSort() on data.
• Print the sorted array using
printArray().

STEP 5. Compile and Run:

•Compile and execute the program


to display the sorted array in
ascending order.

You might also like