Ccl212!18!2 Bsemc 2 Ola f4 Gaila Andrei Lloyd L.
Ccl212!18!2 Bsemc 2 Ola f4 Gaila Andrei Lloyd L.
Ccl212!18!2 Bsemc 2 Ola f4 Gaila Andrei Lloyd L.
java
CCL212-18_ 2BSEMC2 _OLA-F4_ Gaila, Andrei Lloyd L.pdf
1. Bubble
2. Insertion
3. Selection
4. Exit
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)
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
Source Code
import java.util.Arrays;
import java.util.Scanner;
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