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
30 changes: 30 additions & 0 deletions Data_Structure_Using_JavaProgramming/Array_Programs/Minimum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Find minimum element in a sorted and rotated array

package Data_Structure_Using_JavaProgramming.Array_Programs;

public class Minimum {

public static void main(String[] args) {
int arr[]={16,19,21,25,3,5,8,10};
System.out.println("Minimum element in the array : "+findMinimumElementRotatedSortedArray(arr,0,arr.length-1,5));
}
public static int findMinimumElementRotatedSortedArray(int[] arr,int low,int high,int number)
{
int mid;
while(low<high)
{
mid=low + ((high - low) / 2);

if(arr[mid] > arr[high])
{
low=mid+1;
}
else
{
high=mid;
}
}
return arr[low];
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Java program to find missing number in an array.
//Solution:
// Find the sum of n number using formula n=n*(n+1)/2
// Find the sum of elements present in given array.
// Substract (sum of n numbers – sum of elements present in the array).

package Data_Structure_Using_JavaProgramming.Array_Programs;

class Missingnumber {

public static void main(String[] args) {

int[] arr1={7,5,6,1,4,2};
System.out.println("Missing number from array arr1: "+missingNumber(arr1));
int[] arr2={5,3,1,2};
System.out.println("Missing number from array arr2: "+missingNumber(arr2));

}

public static int missingNumber(int[] arr)
{
int n=arr.length+1;
int sum=n*(n+1)/2;
int restSum=0;
for (int i = 0; i < arr.length; i++) {
restSum+=arr[i];
}
int missingNumber=sum-restSum;
return missingNumber;
}
}

22 changes: 22 additions & 0 deletions Data_Structure_Using_JavaProgramming/Array_Programs/SortArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package Data_Structure_Using_JavaProgramming;

// Sort An Array In Java:
// Method 1: Using java.util.Arrays

import java.util.Arrays;
public class SortArray {
public static void main (String [] args) {
int [] array = {45,12,85,32,89,39,69,44,42,1,6,8}; // Enter Your Input Here
Arrays.sort(array);
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
};
}

// java.util.Arrays is a library, included in Java programming language.
// use of sort method which will save us time by not requiring us to manually
// create the loops for sorting but do it automatically.



92 changes: 92 additions & 0 deletions Data_Structure_Using_JavaProgramming/Stack/Sortstack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Sort a stack using another stack in java.

package Data_Structure_Using_JavaProgramming.Stack;

public class Sortstack {
int size;
int arr[];
int top;

Sortstack(int size) {
this.size = size;
this.arr = new int[size];
this.top = -1;
}

public void push(int pushedElement) {
if (!isFull()) {
top++;
arr[top] = pushedElement;
} else {
System.out.println("Stack is full !");
}
}

public int pop() {
if (!isEmpty()) {
int returnedTop = top;
top--;
return arr[returnedTop];

} else {
System.out.println("Stack is empty !");
return -1;
}
}

public int peek() {
return arr[top];
}

public boolean isEmpty() {
return (top == -1);
}

public boolean isFull() {
return (size - 1 == top);
}

public static void main(String[] args) {
Sortstack Sortstack = new Sortstack(10);
System.out.println("=================");
Sortstack.push(10);
Sortstack.push(30);
Sortstack.push(50);
Sortstack.push(40);
Sortstack.printStack(Sortstack);
Sortstack sortedStack=sortStack(Sortstack);
System.out.println("=================");
System.out.println("After Sorting :");
System.out.println("=================");
sortedStack.printStack(sortedStack);

}

// Sort a stack using another stack
public static Sortstack sortStack(Sortstack stack)
{
Sortstack tempStack = new Sortstack(10);
while(!stack.isEmpty())
{
int currentData=stack.pop();
while(!tempStack.isEmpty() && tempStack.peek() > currentData) {
stack.push(tempStack.pop());
}
tempStack.push(currentData);
}
return tempStack;
}

public void printStack(Sortstack stack)
{
if(top>=0)
{
System.out.println("Elements of stacks are:");
for (int i = 0; i <= top; i++) {
System.out.println(arr[i]);
}
}

}
}

71 changes: 71 additions & 0 deletions Data_Structure_Using_JavaProgramming/Stack/Stackpro.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//Stack implementation using Array in java

package Data_Structure_Using_JavaProgramming.Stack;

public class Stackpro {
int size;
int arr[];
int top;

Stackpro(int size) {
this.size = size;
this.arr = new int[size];
this.top = -1;
}

public void push(int element) {
if (!isFull()) {
top++;
arr[top] = element;
System.out.println("Pushed element:" + element);
} else {
System.out.println("Stack is full !");
}
}

public int pop() {
if (!isEmpty()) {
int topElement = top;
top--;
System.out.println("Popped element :" + arr[topElement]);
return arr[topElement];

} else {
System.out.println("Stack is empty !");
return -1;
}
}

public int peek() {
if(!this.isEmpty())
return arr[top];
else
{
System.out.println("Stack is Empty");
return -1;
}
}

public boolean isEmpty() {
return (top == -1);
}

public boolean isFull() {
return (size - 1 == top);
}

public static void main(String[] args) {
Stackpro Stackpro = new Stackpro(5);
Stackpro.pop();
System.out.println("=================");
Stackpro.push(100);
Stackpro.push(90);
Stackpro.push(10);
Stackpro.push(50);
System.out.println("=================");
Stackpro.pop();
Stackpro.pop();
Stackpro.pop();
System.out.println("=================");
}
}