You are on page 1of 6

Filenames: CCL212-18_2 BSEMC 2_OLA-F4_Gaila, Andrei Lloyd L.

java
CCL212-18_ 2BSEMC2 _OLA-F4_ Gaila, Andrei Lloyd L.pdf

Here is a sample output:

Enter the size of an array : 5


Enter 5 values: 3 1 5 4 2
Menu

1. Bubble
2. Insertion
3. Selection
4. Exit

Enter Choice [1..4]

Create user defined methods for each operation. Display the step by step process

1. BUBBLE
3 1 5 4 2
Algorithm (Bubble Sort)
1 3 5 4 2
1 3 5 4 2 Let A be a linear array of n numbers, swap is
1 3 4 5 2
temporary variable for swapping (or interchange) the
1 3 4 2 5
position of the numbers.
Second pass
1 3 4 2 5 1. input n numbers of an array A
1 3 4 2 5
1 3 4 2 5 2. initialise i equal to 0 and repeat through sub
1 3 2 4 5 steps if i is less than n
1 3 2 4 5 a. initialise j equal to 0 and repeat through
Third Pass
sub steps if j is less than n – 1
1 3 2 4 5 b. do sub steps if A[j] is greater than A[j+1]
1 3 2 4 5 i. assign A[j] to swap
1 2 3 4 5
ii. assign A[j+1] to A[j]
1 2 3 4 5
1 2 3 4 5 iii. assign swap to A[j+1]
3. display the sorted numbers of array A
2. INSERTION 4. exit
Algorithm (Insertion Sort)

Step 1 − If it is the first element, it


is already sorted. return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to
be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted

3 1 5 4 2
1 3 5 4 2
1 3 5 4 2
1 3 4 5 2
1 2 3 4 5

3. SELECTION
Algorithm (Selection Sort)
Let A be a linear array of n numbers, min is the variable to store smallest number and
index is the index location of the smallest element of the array.
1. input n numbers of an array A
2. initialise i equal to 0 and repeat through sub steps if i is less than n – 1
a. assign A[i] to min
b. assign i to index
c. initialise j equal i plus 1 and repeat through sub steps if j is less than n
i. do the sub steps if A[j] is less than min
1. assign A[j] to min
2. assign j to index
ii. increment the value of j
d. assign A[i] to A[index]
e. assign min to A[i]
f. increment the value of i
3. display the sorted numbers of array A
4. exit

3 1 5 4 2
1 3 5 4 2
1 2 5 4 3
1 2 3 4 5
1 2 3 4 5

4. EXIT - Display the Programmer’s Name ( LastName, Firstname)

Source Code

import java.util.Arrays;
import java.util.Scanner;

public class string {

public static void main(String[]args) {

Scanner sc = new Scanner(System.in);


int option;
System.out.print("Enter the size of an array: ");
int sort = sc.nextInt();
int array[] = new int[sort];
do {
System.out.println("menu:");
System.out.println("1- Bubble");
System.out.println("2- Insertion");
System.out.println("3- Selection");
System.out.println("4- Exit");
System.out.print("Enter: ");
option = sc.nextInt();
System.out.println();

switch (option) {
case 1:
int swap = 0;
System.out.println("Enter " + sort + " number: ");
for (int i = 0; i < sort; i++) {
array[i] = sc.nextInt();
}
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length - 1; j++) {
if (array[j] > array[j + 1]) {
swap = array[j];
array[j] = array[j + 1];
array[j + 1] = swap;
}
for (int k = 0; k < array.length; k++) {
System.out.print(array[k] + "\t");
}
System.out.println();
}
System.out.println();
}
break;
case 2:
System.out.println("Enter " + sort + " number: ");
for (int i = 0; i < sort; i++) {
array[i] = sc.nextInt();
}
for (int j = 1; j < array.length; j++) {
int key = array[j];
int i = j - 1;
while ((i > -1) && (array[i] > key)) {
array[i + 1] = array[i];
i--;
}
array[i + 1] = key;
}
for (int k : array) {
System.out.print(k + " ");
}
System.out.println();
break;
case 3:
int min=0;
int index=0;
System.out.println("Enter " + sort + " number: ");
for (int i = 0; i < sort; i++) {
array[i] = sc.nextInt();
}
for (int i = 0; i < array.length - 1; i++) {
min = array[i];
index = i;
for(int j = i+1; j < array.length; j++) {
if(array[j] < min) {

min = array[j];
index = j;
}
}
array[index] = array[i];
array[i] = min;
}
for(int i: array) {
System.out.print(i + " ");
}
System.out.println();
break;
}
} while (option != 4);
System.out.println("Programmer's Name: Gaila,Andrei Lloyd L.");
}

}
Screenshot
ALGORITHM
INSERTING AN ELEMENT TO CIRCULAR QUEUE
1. Initialize FRONT = -1; REAR = -1
2. REAR = (REAR + 1) % SIZE
3. If (FRONT is equal to REAR)
(a) Display “Queue is full” (b) Exit
4. Else
Input the value to be inserted and assign to variable “DATA”
5. If (FRONT is equal to – 1)
FRONT = 0 and REAR = 0
6. Q[REAR] = DATA
7. Repeat steps 2 to 5 , to add elements
8. Exit

You might also like