Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Section B/CPW/datastructure/Bubble Sort/BubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.Scanner;

public class Bubble{

public static void main(String[] args){

int Numbers[]=new int[10];
int N=10;
int outer,inner,temp;
Scanner input = new Scanner(System.in);

System.out.println("Enter ten numbers for sorting:");
for(outer=0;outer<10;outer++) //To get values from user
Numbers[outer]=input.nextInt();


for(outer=1;outer<N;outer++) //To sort the array
{
for(inner=0;inner<N-outer;inner++)
{
if(Numbers[inner]>Numbers[inner+1])
{
temp=Numbers[inner];
Numbers[inner]=Numbers[inner+1];
Numbers[inner+1]=temp;
}
}
}

System.out.println("Your numbers was ordered!") ;

for(outer=0;outer<10;outer++) //To show the ordered numbers
System.out.print(Numbers[outer]+" ");


}
}
85 changes: 85 additions & 0 deletions Section B/CPW/datastructure/Quick Sort/QuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*---------------------------------------------------
This code copied from:
DATA STRUCTURES & ALGORITHMS IN JAVA
ROBERT LAFORE
SECOND EDITION


-----------------------------------------------------*/
class ArrayIns{
private long[] theArray; //ref to array theArray
private int nElems; //number of data items

public ArrayIns(int max) //constructor
{
theArray =new long[max]; //create the array
nElems =0;
}

public void insert(long value) //put elements into array
{
theArray[nElems] = value; //insert items
nElems++; //increament size
}

public void display() //display array contents
{
System.out.print(" A= ");
for(int j=0;j<nElems;j++)
System.out.print(theArray[j]+" ");
System.out.println("");
System.out.println("");
}

public void quickSort()
{
recQuickSort(0,nElems-1);
}

public void recQuickSort(int left, int right)
{
if(right-left<=0) //if size <= 1
return; //already sorted
else //size is 2 or larger
{
long pivot = theArray[right]; //rightmost item

int partition = partitionIt(left,right,pivot);

recQuickSort(left,partition-1); //sort left side
recQuickSort(partition+1,right); //sort right side

}
}

public int partitionIt(int left, int right, long pivot)
{
int leftPtr = left-1; //left (after ++)
int rightPtr = right; //right-1 (after --)
while(true)
{ //find bigger item
while(theArray[++leftPtr] < pivot)
;
//find smaller item
while(rightPtr > 0 && theArray[--rightPtr] > pivot)
;

if(leftPtr>=rightPtr) //if pointers cross
break; //partition done
else //not crossed
swap(leftPtr,rightPtr); //swap elements
}

swap(leftPtr,right); //restore pivot
return leftPtr; //return pivot location

}

public void swap(int dex1, int dex2) //swap two elements
{
long temp = theArray[dex1];
theArray[dex1] = theArray[dex2];
theArray[dex2] = temp;
}

}
20 changes: 20 additions & 0 deletions Section B/CPW/datastructure/Quick Sort/QuickSortApp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class QuickSortApp
{
public static void main(String[] args)
{
int maxSize = 16; //array maxSize
ArrayIns arr;
arr = new ArrayIns(maxSize); //create array

for(int j=0; j<maxSize;j++) //fill array with random numbers
{
long n = (int)(java.lang.Math.random()*99);
arr.insert(n);
}

arr.display(); //display items before sort
arr.quickSort(); //sort items
arr.display(); //display items after sort
}

}