CC 104-Project - Final

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

CC 104: Data Structure and Algorithms

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 {

static int bubbleswap=0;

static int insertionswap=0;

static int selectionswap = 0;

public static void main(String[] args) throws IOException

BufferedReader br = new BufferedReader(new


InputStreamReader(System.in));

System.out.println("Enter number to be sorted");

int n = Integer.parseInt(br.readLine());

int a[] = new int[n];

int b[] = new int[n];

int c[] = new int[n];

System.out.println("Enter "+n+" elements into an array");

for(int i = 0; i<n; i++)

String hexa = br.readLine();

a[i] = Integer.parseInt(hexa, 16);

b[i] = Integer.parseInt(hexa, 16);

c[i] = Integer.parseInt(hexa, 16);

System.out.println("Before sorting: ");


System.out.println(Arrays.toString(a));
bubblesort(a, n);
selectionsort(b, n);
insertionsort(c, n);
System.out.println("After sorting: \n");
System.out.println("Bubble Sorting");
System.out.println(Arrays.toString(a));
System.out.println("Swapping:" + bubbleswap +"\n");
System.out.println("Selection Sorting");
System.out.println(Arrays.toString(b));
System.out.println("Swapping:" + selectionswap +"\n");
System.out.println("Insertion Sorting");
System.out.println(Arrays.toString(c));
System.out.println("Swapping:" + insertionswap +"\n");
}
public static void bubblesort(int[] a, int n)
{
int x, j, temp;
for(x=n-2;x>=0;x--)
{
for(j=0;j<=x;j++)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j + 1];
a[j+1] = temp;
bubbleswap++;
}
}
}
}
public static void selectionsort(int[] b, int n)
{
int minIndex, temp, i, j;
for(i=0;i<n-1;i++)
{
minIndex = i;
for(j=i+1;j<n;j++)
{
if(b[j]<b[minIndex])
minIndex = j;
}
if(i!=minIndex)
{
temp=b[i];
b[i] = b[minIndex];
b[minIndex] = temp;
selectionswap++;
}
}
}
public static void insertionsort(int[] c, int n)
{
int i, j, temp;
for(i=1;i<n;i++)
{
temp = c[i];
for(j=i-1;j>=0 && c[j]>temp; j--)
{
c[j+1] = c[j];
}
c[j+1] = temp;
insertionswap++;
}
}
}

OUTPUT:

Enter number to be sorted

Enter 5 elements into an array

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()

{ Scanner integer=new Scanner(System.in);

System.out.println("Enter stack size:");

maxsize=integer.nextInt();

st=new int[maxsize];

top=-1;

public void push(int element)

{ /*if(top>=maxsize)

{ System.out.println("Overflow!!");

//return(0);

}*/

try

{ st[++top]=element;

catch(ArrayIndexOutOfBoundsException e)

{ System.out.println(e);

public int pop()

{ if(top==-1)
{ System.out.println("UnderFlow");

return(-1);

return(st[top--]);

public void display(int[] st, int max_size)

{ int i;

System.out.println("Stack Elements:");

for(i=0;i<=max_size;i++)

System.out.println(st[i]);

/*myStack.java*/

class myStack

static int option;

public static void main(String[] args)

{ stack obj=new stack();

while(true)

{ System.out.println("\nEnter yours choice\n1.PUSH\n2.POP\n3.Display\


n4..EXIT");

Scanner integer=new Scanner(System.in);

option=integer.nextInt();

switch(option)

{ case 1: System.out.println("Enter Element");

obj.element=integer.nextInt();

obj.push(obj.element);

break;

case 2:System.out.printf("Poped element is %d",obj.pop());

break;

case 3:obj.display(obj.st,obj.top);
break;

case 4:System.exit(0);

default:System.out.println("Wrong option");

OUTPUT:

Enter stack size:

Enter yours choice

1.PUSH

2.POP

3.Display

4..EXIT

Enter Element

Enter yours choice

1.PUSH

2.POP

3.Display

4..EXIT

Enter Element

15

Enter yours choice

1.PUSH

2.POP

3.Display

4..EXIT

Poped element is 15

Enter yours choice

1.PUSH

2.POP
3.Display

4..EXIT

Poped element is 8

Enter yours choice

1.PUSH

2.POP

3.Display

4..EXIT

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