M Labexer5 Sacasas Elizalde

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Sacasas, Elizalde

ITC C202-201DA
Prelim Laboratory Exercise 5: Sorting Algorithm Implementation

Sorting Algorithm: Insertion Sort

REQUIREMENTS:

Input/s:  Size of an array and number of elements

Output/s: The sorted array in ascending order

DESIGN:

Algorithm:

Step 1: InsertionSort (array, size)


Step 2: Initialize n, step, j, element
Step 3: Input size of an array and the elements
Step 4: Mark first element as sorted
Step 5: For each unsorted element X
'extract' the element X
Step 6: For j <- lastSortedIndex down to 0
Step 7: If current element j > X
move sorted element to the right by 1
Step 8: Break loop and insert X here
Step 9: End

SOURCE CODE:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {

int n, step, j, element;

try (Scanner scan = new Scanner(System.in)) {

System.out.print("Enter the Size of Array: ");

n = scan.nextInt();
int[] array = new int[n];

System.out.print("Enter " +n+ " Elements: ");

for(step = 0; step < n; step++)


array[step] = scan.nextInt();

for(step = 1; step < n; step++) {


element = array[step];

for(j = (step-1); j >= 0 && array[j] > element; j--)


array[j+1] = array[j];

array[j+1] = element;
}

System.out.println("\nSorted Array ASC order is: ");


for(step = 0; step < n; step++)
System.out.print(array[step]+ " ");
}
}

}
SCREENSHOTS:

Sorting Algorithm: Selection Sort

REQUIREMENTS:

Input/s:  Number of elements

Output/s: The sorted array in ascending order

DESIGN:

Algorithm:

Step 1: SortingAlgorithm (array, size)


Step 2: Initialize num, step, j, count, size, min_idx = 0
Step 3: Input number of elements
Step 4: Set the first unsorted element as the minimum
Step 5: For each of the unsorted elements
if element < currentMinimum
Step 6: Set element as new minimum
Step 7: Swap minimum with first unsorted position
Step 8: End

SOURCE CODE:
import java.util.Scanner;

public class SortingAlgorithm {


public static void main(String[] args)
{
int num, step, j, count, size, min_idx = 0;

try (Scanner scan = new Scanner(System.in)) {

System.out.print("Enter the Size of Array: ");


num = scan.nextInt();
int[] array = new int[num];

System.out.print("Enter " +num+ " Elements for the Array: ");

for(step = 0; step < num; step++)


array[step] = scan.nextInt();

for(step = 0; step < (num-1); step++) {


count = 0;
size = array[step];

for(j = (step+1); j < num; j++) {


if(size > array[j]) {
size = array[j];
count++;
min_idx = j;
}
}

if(count != 0) {
int temp = array[step];
array[step] = size;
array[min_idx] = temp;
}
}

System.out.println("\nSorted Array ASC order is: ");


for(step = 0; step < num; step++)
System.out.print(array[step]+ " ");
}

SCREENSHOTS:

Sorting Algorithm: Bubble Sort

REQUIREMENTS:

Input/s:  Size of an array and, number of elements

Output/s: The sorted array in ascending order


DESIGN:

Algorithm:

Step 1: BubbleSort (array, size)


Step 2: Initialize n, step, size
Step 4: Input size of an array and the elements
Step 3: Run a nested for loop to traverse the input array using two variables i and j, such that 0
≤ i < n-1 and 0 ≤ j < n-i-1
Step 4: If arr[j] is greater than arr[j+1] then swap these adjacent elements, else move on
Step 5: Print the sorted array
Step 6: End

SOURCE CODE:
import java.util.Scanner;

public class Main {


public static void main(String[] args)
{
try (Scanner s = new Scanner(System.in)) {
System.out.print("Enter the Size of Array: ");
int n = s.nextInt();

int[] array = new int[n];

System.out.print("Enter " +n+" Elements: ");


for(int step = 0; step < n; step++)
array[step] = s.nextInt();

for(int step=0; step<(n-1); step++) {


for(int j=0; j<(n-step-1); j++) {
if(array[j]>array[j+1]) {
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}

System.out.println("\nSorted Array Ascending order is: ");


for(int step = 0; step < n; step++)
System.out.print(array[step]+ " ");
}
}
}

SCREENSHOTS:

Sorting Algorithm: Bucket Sort

REQUIREMENTS:

Input/s:  Size of an array and number of elements

Output/s: The sorted array in ascending order

DESIGN:

Algorithm:

Step 1: BucketSort (array, size)


Step 2: Initialize
Step 3: Input size of an array and the elements
Step 4: Create N buckets each of which can hold a range of values
Step 5: for all the buckets
Step 6: Initialize each bucket with 0 values
Step 7: For all the buckets
Step 8: Put elements into buckets matching the range
Step 9: For all the buckets
Step 10: Sort elements in each bucket
Step 11: Gather elements from each bucket
Step 12: End

SOURCE CODE:

SCREENSHOTS:

Sorting Algorithm: Merge Sort

REQUIREMENTS:

Input/s:  Size of an array and number of elements

Output/s: The sorted array in ascending order

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;

public class Main {


public static void merge(int a[], int l, int m, int h)
{
int i, j, c = l;
int b[] = new int[h+1];

for(i = l, j = m+1; i <= m && j <= h; c++) {

if(a[i] <= a[j])


b[c] = a[i++];
else
b[c] = a[j++];
}

while(i <= m )
b[c++] = a[i++];

while(j<=h)
b[c++] = a[j++];

for(i = l ; i <= h; i++)


a[i] = b[i];
}

public static void Sort(int a[], int l, int h) {


if(l < h) {
int m = (l+h)/2;
Sort(a,l,m);
Sort(a,m+1,h);
merge(a,l,m,h);
}
}

public static void printarray(int a[]) {


for(int i = 0; i < a.length; i++) {
System.out.print(a[i]+" ");
}

}
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:

Sorting Algorithm: Quick Sort

REQUIREMENTS:

Input/s:  Size of an array and number of


elements

Output/s: The sorted array in ascending order

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;

public class Main {


public static int partition(int a[],int l,int h) {
int i = l+1 ,j = h, c = l, temp;

for(; i <= j ;) {

while(i <= h && a[i] < a[c])


i++;

while(a[j] > a[c] && j > l)


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;
}

public static void Sort(int a[], int l, int h) {


if(l < h) {
int m = partition(a, l, h);
Sort(a, l, m-1);
Sort(a, m+1, h);
}

public static void printarray(int a[]) {


for(int i=0; i < a.length; i++) {
System.out.print(a[i]+" ");
}

public static void main(String[] args) {


int n, res,i;

try (Scanner s = new Scanner(System.in)) {

System.out.print("Enter the size of an 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:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy