M Labexer5 Sacasas Elizalde
M Labexer5 Sacasas Elizalde
M Labexer5 Sacasas Elizalde
ITC C202-201DA
Prelim Laboratory Exercise 5: Sorting Algorithm Implementation
REQUIREMENTS:
DESIGN:
Algorithm:
SOURCE CODE:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
n = scan.nextInt();
int[] array = new int[n];
array[j+1] = element;
}
}
SCREENSHOTS:
REQUIREMENTS:
DESIGN:
Algorithm:
SOURCE CODE:
import java.util.Scanner;
if(count != 0) {
int temp = array[step];
array[step] = size;
array[min_idx] = temp;
}
}
SCREENSHOTS:
REQUIREMENTS:
Algorithm:
SOURCE CODE:
import java.util.Scanner;
SCREENSHOTS:
REQUIREMENTS:
DESIGN:
Algorithm:
SOURCE CODE:
SCREENSHOTS:
REQUIREMENTS:
DESIGN:
Algorithm:
Step 1: Start
Step 2: Declare array and left, right, mid variable
Step 3: perform merge function.
if left > right
return
mid= (left+right)/2
mergesort(array, left, mid)
mergesort(array, mid+1, right)
merge(array, left, mid, right)
Step 4: End
SOURCE CODE:
import java.util.Scanner;
while(i <= m )
b[c++] = a[i++];
while(j<=h)
b[c++] = a[j++];
}
public static void main(String[] args)
{
int n, res, i;
try (Scanner s = new Scanner(System.in)) {
System.out.print("Enter number of elements in the array: ");
n = s.nextInt();
int a[] = new int[n];
System.out.print("Enter "+n+" elements: ");
for( i=0; i < n; i++)
{
a[i] = s.nextInt();
}
Sort(a, 0, n-1);
System.out.println( "\nSorted Array Ascending Order is: ");
printarray(a);
}
}
SCREENSHOTS:
REQUIREMENTS:
DESIGN:
Algorithm:
Step 1: partition(array, leftmostIndex, rightmostIndex)
Step 2: Initialize i = l+1 ,j = h, c = l, temp
Step 3: Set rightmostIndex as pivotIndex
storeIndex <- leftmostIndex – 1
Step 4: For i <- leftmostIndex + 1 to rightmostIndex
Step 5: If element[i] < pivotElement
swap element[i] and element[storeIndex]
storeIndex++
swap pivotElement and element[storeIndex+1]
Step 6: Return storeIndex + 1
Step 7: End
SOURCE CODE:
import java.util.Scanner;
for(; i <= j ;) {
if(i < j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else
break;
}
temp = a[c];
a[c] = a[j];
a[j] = temp;
return j;
}
n = s.nextInt();
int a[] = new int[n];
Sort(a,0,n-1);
System.out.println( "\nSorted Array Ascending Order is: ");
printarray(a);
}
}
SCREENSHOTS: