Hi all, still am having trouble with this i know about arrays, looking for help
ID: 3697715 • Letter: H
Question
Hi all, still am having trouble with this i know about arrays, looking for help in break down how to write in pseudo code by trying to create an array with 20 integer elements, namely array1, array2, array 3, and then I need to pass all three array through sorting methods using bubble sort, selection sort and insertion sort passing all three as arguments. also must count the number of swaps during thes sorting of the two elements for all three sorting methods and at the ened to display the number of swap counts. there is example on here using java, as i don't know java and this introduction programming using the pseudo code and and flow charts. i am having trouble trying construct this together. rather confused. any help using psedu as example using one of the one swap methods to show how construct this would sure help me alot.
Explanation / Answer
Answer for Question:
1. This below java code has three sorting methods bubble sort , insertion sort and selection sort.
2. each sort method will take the array of elements and count the number of swaps taken place will return to main methid.
3. In main method will print the number of swaps in each sort.
You can change the number of elements i have hard coded the elements
Refer the below code :
function bubblesort( var a as array )
for i from 1 to N
swaps = 0
for j from 0 to N - i
if a[j] > a[j + 1]
swap( a[j], a[j + 1] )
swaps = swaps + 1
return swaps;
end function
function selctionsort(var a as array)
max = length(array) - 1
for i from 0 to max
swaps = 0
key = array[i]
keyj = i
for j from i+1 to max
if array[j] < key
key = array[j]
keyj = j
swaps = swaps + 1
array[keyj] = array[i]
array[i] = key
return swaps;
end function
function insertionsort(var a as array)
swaps = 0
for i 1 to length(array)
j i
while j > 0 and A[j-1] > A[j]
swap A[j] and A[j-1]
swaps = swaps + 1
j j - 1
end while
end for
return swaps;
end function
These above the pseudo code for bubble sort, selctionsort and insertionsort will return the number of swaps are done
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.