-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75b92b9
commit 5b0c8c4
Showing
2 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import java.util.*; | ||
public class a3q17 | ||
{ | ||
public static void main(String args[]) | ||
{ | ||
Scanner in = new Scanner(System.in); | ||
System.out.print("Enter the size of the array :"); | ||
int n=in.nextInt(); | ||
int arr[] = new int[n]; | ||
System.out.println("Enter the Elements in the array :"); | ||
for(int i=0; i<n; i++) | ||
{ | ||
arr[i]=in.nextInt(); | ||
} | ||
System.out.print("Enter the pivot element :"); | ||
int p=in.nextInt(); | ||
int p1=0, p2=n-1; | ||
int[] s=quicksort(arr, p, p1, p2); | ||
for(int i=0; i<n; i++) | ||
{ | ||
System.out.print(arr[i]); | ||
} | ||
} | ||
public static int[] quicksort(int arr[], int p, int p1, int p2 ) | ||
{ | ||
if(p1<p2) | ||
{ | ||
if(arr[p1]<p) | ||
p1++; | ||
else | ||
{ | ||
if(arr[p2]>p) | ||
p2--; | ||
else if(arr[p1]==arr[p2]) | ||
p1++; | ||
else | ||
{ | ||
int t=arr[p1]; | ||
arr[p1]=arr[p2]; | ||
arr[p2]=t; | ||
} | ||
} | ||
return quicksort(arr,p,p1,p2); | ||
} | ||
else | ||
return arr; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
import java.util.*; | ||
public class a3q18 | ||
{ | ||
public static void main(String args[]) | ||
{ | ||
queue arr[] = new queue[3]; | ||
Scanner in = new Scanner(System.in); | ||
System.out.print("Enter the Number of Rings :"); | ||
int n=in.nextInt(); | ||
for(int i=0; i<3; i++) | ||
{ | ||
arr[i]= new queue(n, "Queue "+i); | ||
} | ||
for(int i=n; i>=1; i--) | ||
{ | ||
arr[0].push(i); | ||
System.out.println(arr[0]); | ||
} | ||
set(n, arr, 0, 1, 2); | ||
} | ||
public static void set(int i, queue[] arr, int q1, int q2, int q3) | ||
{ | ||
if(i>0) | ||
{ | ||
set(i-1, arr, q1, q3, q2); | ||
swapDisk(q1, q2, arr); | ||
for(queue t:arr) | ||
{ | ||
System.out.println(t.getName()+" :"+t); | ||
} | ||
set(i-1, arr, q3, q2, q1); | ||
} | ||
} | ||
public static void swapDisk(int q1, int q2, queue[] arr) | ||
{ | ||
int t=arr[q1].peek(); | ||
if(t>0) | ||
{ | ||
arr[q2].push(t); | ||
arr[q1].pop(); | ||
} | ||
} | ||
} | ||
class queue | ||
{ | ||
private int arr[]; | ||
private String name; | ||
int top; | ||
queue(int n, String name) | ||
{ | ||
this.name=name; | ||
top=-1; | ||
arr = new int[n]; | ||
} | ||
String getName() | ||
{ | ||
return name; | ||
} | ||
void pop() | ||
{ | ||
if(top==-1) | ||
{ | ||
try | ||
{ | ||
throw new StackUnderflowException("Queue is already Empty"); | ||
} | ||
catch(StackUnderflowException e) | ||
{ | ||
//e.printStackTrace(); | ||
} | ||
} | ||
else | ||
top--; | ||
} | ||
int peek() | ||
{ | ||
if(top!=-1) | ||
{ | ||
return arr[top]; | ||
} | ||
else | ||
{ | ||
//System.out.print("Queue is Empty, Nothing to show"); | ||
return -1; | ||
} | ||
} | ||
void push(int i) | ||
{ | ||
if(top==arr.length) | ||
{ | ||
try | ||
{ | ||
throw new StackOverflowException("Queue is already full"); | ||
} | ||
catch(StackOverflowException e) | ||
{ | ||
//e.printStackTrace(); | ||
} | ||
} | ||
else | ||
{ | ||
top++; | ||
arr[top]=i; | ||
} | ||
} | ||
int[] show() | ||
{ | ||
if(top==-1) | ||
{ | ||
try | ||
{ | ||
throw new QueueEmptyException("Queue is empty Nothing to Show"); | ||
} | ||
catch(QueueEmptyException e) | ||
{ | ||
//e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
else | ||
{ | ||
int rt[] = new int[top+1]; | ||
for(int i=0; i<=top; i++) | ||
{ | ||
rt[i]=arr[i]; | ||
} | ||
return rt; | ||
} | ||
} | ||
public String toString() | ||
{ | ||
if(show()!=null) | ||
{ | ||
int[] rt=show(); | ||
String s="["; | ||
for(int i:rt) | ||
{ | ||
s=s+i+", "; | ||
} | ||
s=s+"]"; | ||
return s; | ||
} | ||
else | ||
return "[ ]"; | ||
} | ||
} | ||
class StackUnderflowException extends Exception | ||
{ | ||
StackUnderflowException(String s) | ||
{ | ||
super(s); | ||
} | ||
} | ||
class StackOverflowException extends Exception | ||
{ | ||
StackOverflowException(String s) | ||
{ | ||
super(s); | ||
} | ||
} | ||
class QueueEmptyException extends Exception | ||
{ | ||
QueueEmptyException(String s) | ||
{ | ||
super(s); | ||
} | ||
} |