0% found this document useful (0 votes)
2 views

Algorithm Sorting DSA_LAB

The document outlines algorithms for printing an array and sorting it using Bubble Sort, Selection Sort, or Insertion Sort. It includes steps for initializing the array, reading its size and elements, and executing the chosen sorting algorithm with appropriate print statements. If an invalid choice is made, it prompts the user for valid input options.

Uploaded by

merlinjr.csb2226
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Algorithm Sorting DSA_LAB

The document outlines algorithms for printing an array and sorting it using Bubble Sort, Selection Sort, or Insertion Sort. It includes steps for initializing the array, reading its size and elements, and executing the chosen sorting algorithm with appropriate print statements. If an invalid choice is made, it prompts the user for valid input options.

Uploaded by

merlinjr.csb2226
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Algorithm printArray(A,n)

Step 1: Start

Step 2: From i=0 to n-1 Repeat

Step 2.1: print A[i]

Step 3: Stop

Algorithm Main()

Step 1: Start

Step 2: Initialize an Array, A

Step 3: Read the size of array, N

Step 4: Read the elements into the array A

Step 5: Enter the choice 1. Bubble sort 2. Selection Sort 3. Insertion Sort and store the choice in a
variable, ch

Step 6: if ch==1 then,

Step 6.1: print Array Before sorting using printArray()

Step 6.2: From i=0 to n-2 repeat the following

Step 6.2.1: From j=0 to n-2-i repeat the following

Step 6.2.1.1: if(A[j]>A[j+1])then,

Step 6.2.1.1.1: Swap(A[j] and A[j+1])

Step 6.2.2: print the current status of the array

Step 6.3: Print the array after sorting using printArray()

Step 7: if(ch==2) then,

Step 7.1:print the array before sorting using the printArray()

Step 7.2: From i=0 to n-1 Repeat the following

Step 7.2.1: Set min=i

Step 7.2.2: From j=i+1 to n Repeat the following

Step 7.2.2.1: if(A[j]<A[min]) then,

Step 7.2.2.1.1: Set min=j

Step 7.2.3: Swap(A[i] and A[min])


Step 7.2.4: print the current status of the array

Step 7.3: print the final array after sorting

Step 8: if(ch==3) then,

Step 8.1: print the array before sorting using printArray()

Step 8.2: From i=0 to n-1 repeat the following

Step 8.2.1: Set j=i

Step 8.2.2: Repeat until j>0 and Arr[j-1]>Arr[j]

Step 8.2.2.1: Swap(Arr[j] and Arr[j-1])

Step 8.2.2.2: j - -

Step 8.2.3: print the current status of the array

Step 8.3: print the final array after sorting

Step 9: Else

Step 9.1: Invalid Input Enter either 1 2 or 3

Step 10: Stop

You might also like