In: Computer Science
I need a pseudocode and UML for this program.
import java.util.Random;
public class SortandSwapCount {
public static void main(String[] args) {
//Generate random array for test
and copy that into 3 arrays
int[] arr1=new int[20];
int[] arr2=new int[20];
int[] arr3=new int[20];
int[] arr4=new int[20];
Random rand = new Random();
for (int i = 0; i < arr1.length;
i++) {
//Generate
random array
arr1[i] = rand.nextInt()%20;
//Copy
arr2[i]=arr3[i]=arr4[i]=arr1[i];
}
System.out.println("\n");
//Display insertion sort array and
swap count
int
swapCnt=insertionSort(arr1);
for (int i = 0; i < arr1.length;
i++) {
System.out.print(arr1[i]+" ");
}
System.out.println("\nInsertion
sort swap count = "+swapCnt);
//Display bubble sort array and
swap count
swapCnt=bubbleSort(arr2);
for (int i = 0; i < arr2.length;
i++) {
System.out.print(arr2[i]+" ");
}
System.out.println("\nBubble sort
swap count = "+swapCnt);
//Display merge sort array and swap
count
swapCnt=mergeSort(arr3,0,19);
for (int i = 0; i < arr3.length;
i++) {
System.out.print(arr3[i]+" ");
}
System.out.println("\nMerge sort
swap count = "+swapCnt);
//Display quick sort array and swap
count
swapCnt=quickSort(arr4,0,19);
for (int i = 0; i < arr4.length;
i++) {
System.out.print(arr4[i]+" ");
}
System.out.println("\nQuck sort
swap count = "+swapCnt);
}
//Insertion sort implementation
public static int insertionSort(int[] arr) {
//For swap count
int swapCnt=0;
//Loop until end of the
array
for (int i = 1; i <
arr.length; ++i) {
int temp = arr[i];
int j = i - 1;
//Swapping test
while (j >= 0 && arr[j] >temp) {
arr[j + 1] = arr[j];
//Increment swap count
swapCnt++;
j = j - 1;
}
arr[j + 1] = temp;
}
//Return swap
count
return swapCnt;
}
//Bubble sort implementation
public static int bubbleSort(int[] arr) {
int swapCnt=0;
//Comparison loop
for (int i = 0; i <
arr.length-1; i++)
for (int j = 0; j < arr.length-i-1; j++)
//Compare and swap
if (arr[j] > arr[j+1])
{
//Swap and increment swap count
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swapCnt++;
}
return swapCnt;
}
//Mergesort implementation
public static int mergeSort(int[] arr,int start,int
end) {
int
mid,swapCnt=0;
if(start<end)
{
mid =
(start+end)/2;
swapCnt=
mergeSort(arr,start,mid);
swapCnt=mergeSort(arr,mid+1,end);
swapCnt+=merge(arr,start,mid,end);
}
return
swapCnt;
}
//Merge 2 parts of the array
public static int merge(int arr[], int start, int mid,
int end)
{
int i=start,j=mid+1,k,index =
start,swapCnt=0;
int[] temp=new
int[arr.length];
while(i<=mid &&
j<=end)
{
if(arr[i]<arr[j])
{
temp[index] = arr[i];
swapCnt++;
i = i+1;
}
else
{
temp[index] = arr[j];
j = j+1;
}
index++;
}
if(i>mid)
{
while(j<=end)
{
temp[index] = arr[j];
index++;
j++;
swapCnt++;
}
}
else
{
while(i<=mid)
{
temp[index] = arr[i];
index++;
i++;
swapCnt++;
}
}
k = start;
while(k<index)
{
arr[k]=temp[k];
swapCnt++;
k++;
}
return swapCnt;
}
//Quick sort implemetation
public static int quickSort(int arr[], int
start, int end)
{
int swapCnt=0;
if (start < end)
{
//Partition
int p = arr[end];
int i = (start - 1);
for (int j = start; j <=end - 1; j++) {
if (arr[j] <= p) {
i++;
//Swapping
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
swapCnt++;
}
}
//Swapping
int temp = arr[i + 1];
arr[i + 1] = arr[end];
arr[end] = temp;
swapCnt++;
int pi= i + 1;
//Recursion
swapCnt+=quickSort(arr, start, pi - 1);
swapCnt+=quickSort(arr, pi + 1, end);
}
return swapCnt;
}
}
Output
Insertion sort swap count = 106
-18 -17 -16 -14 -13 -13 -9 -9 -7 -7 -3 -1 0 1 4 6 13 15 16 17
Bubble sort swap count = 106
-18 -17 -16 -14 -13 -13 -9 -9 -7 -7 -3 -1 0 1 4 6 13 15 16 17
Merge sort swap count = 58
-18 -17 -16 -14 -13 -13 -9 -9 -7 -7 -3 -1 0 1 4 6 13 15 16 17
Quick sort swap count = 49
Ans
Main Pseudocode :
1. declear 5 array and pupose to sort
call sort function
buble sort function Pseudocode
procedure bubbleSort( list : array of items )
loop = list.count;
for i = 0 to loop-1 do:
swapped = false
for j = 0 to loop-1 do:
/* compare the adjacent elements */
if list[j] > list[j+1] then
/* swap them */
swap( list[j], list[j+1] )
swapped = true
end if
end for
/*if no number was swapped that means
array is sorted now, break the loop.*/
if(not swapped) then
break
end if
end for
end procedure return list
quick Sort function Pseudo code
procedure quickSort(left, right) if right-left <= 0 return else
leftPointer = left rightPointer = right - 1 while True do while A[++leftPointer] < pivot do //do-nothing end while while rightPointer > 0 && A[--rightPointer] > pivot do //do-nothing end while if leftPointer >= rightPointer break else swap leftPointer,rightPointer end if end while swap leftPointer,right cnt= quickSort(left,partition-1) cnt= quickSort(partition+1,right) end if end procedure
// I write while bescause in code has recusrsive call and then the function back its cursor so for understanding i write this //
mergersort code pseudo code
procedure mergesort( var a as array )
if ( n == 1 ) return a
var l1 as array = a[0] ... a[n/2]
var l2 as array = a[n/2+1] ... a[n]
l1 = mergesort( l1 )
l2 = mergesort( l2 )
return merge( l1, l2 )
end procedure
procedure merge( var a as array, var b as array )
var c as array
while ( a and b have elements )
if ( a[0] > b[0] )
add b[0] to the end of c
remove b[0] from b
else
add a[0] to the end of c
remove a[0] from a
end if
end while
while ( a has elements )
add a[0] to the end of c
remove a[0] from a
end while
while ( b has elements )
add b[0] to the end of c
remove b[0] from b
end while
return c
end procedure
Comment down if any mistake
Comment down if Any Query
Pleause Thumbs uo if you satisfy with Answer