CC 104-Project - Final
CC 104-Project - Final
CC 104-Project - Final
Prefinal Project
1. Write a program that will sort five (5) integer values in descending order using Bubble, Selection, and Insertion
algorithms and display the number of swapping done.
Code:
package pack;
import java.io.*;
import java.util.*;
public class sortingdatastructures {
int n = Integer.parseInt(br.readLine());
OUTPUT:
Before sorting:
[3, 1, 7, 5, 2]
After sorting:
Bubble Sorting
[1, 2, 3, 5, 7]Swapping:5
Selection Sorting[1, 2, 3, 5, 7]
Swapping:3
Insertion Sorting
[1, 2, 3, 5, 7]Swapping:4
2. Implement a stack class for integers using an array to hold the data. User will input for the size of the array,
implement overflow and underflow exceptions for improperly accessing a full or empty stack. Include methods to
push a new item on the stack and to pop (remove) and item from the stack. Add a process to check if the stack is
empty or full.
Code:
import java.io.*;
import java.util.Scanner;
class stack
{ int element,maxsize,top;
int[] st;
public stack()
maxsize=integer.nextInt();
st=new int[maxsize];
top=-1;
{ /*if(top>=maxsize)
{ System.out.println("Overflow!!");
//return(0);
}*/
try
{ st[++top]=element;
catch(ArrayIndexOutOfBoundsException e)
{ System.out.println(e);
{ if(top==-1)
{ System.out.println("UnderFlow");
return(-1);
return(st[top--]);
{ int i;
System.out.println("Stack Elements:");
for(i=0;i<=max_size;i++)
System.out.println(st[i]);
/*myStack.java*/
class myStack
while(true)
option=integer.nextInt();
switch(option)
obj.element=integer.nextInt();
obj.push(obj.element);
break;
break;
case 3:obj.display(obj.st,obj.top);
break;
case 4:System.exit(0);
default:System.out.println("Wrong option");
OUTPUT:
1.PUSH
2.POP
3.Display
4..EXIT
Enter Element
1.PUSH
2.POP
3.Display
4..EXIT
Enter Element
15
1.PUSH
2.POP
3.Display
4..EXIT
Poped element is 15
1.PUSH
2.POP
3.Display
4..EXIT
Poped element is 8
1.PUSH
2.POP
3.Display
4..EXIT