Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue-#6 added some java dsa programs with optimal solution #583

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
40 changes: 40 additions & 0 deletions LeetCode/ShiftByK.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package LEasy;

import java.util.ArrayList;
import java.util.Arrays;

public class ShiftByK {
public static void main(String[] args) {

int[] arr={1,2,3,4,5};
int n=arr.length;
ArrayList<Integer> temp1=new ArrayList<>();

int k=2;

for (int i = k; i < n; i++) {
temp1.add(arr[i]);
}
for (int i = 0; i < k; i++) {
temp1.add(arr[i]);
}

for (int i = 0; i < arr.length; i++) {
arr[i]=temp1.get(i);
}
System.out.println(Arrays.toString(arr));












}

}
61 changes: 61 additions & 0 deletions LeetCode/Sort012.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package LEasy;


import java.util.Arrays;

public class Sort012 {

static class Node{
int data;
Node next;

Node(int data){
this.data=data;
this.next=null;
}
}


public static void main(String[] args) {
int[] arr={0,1,2,0,1,2,1,2,0,0,0,1};
// int n=arr.length;

// bruteforce(mergesort);

optimal(arr);




}

private static void optimal(int[] arr) {
int low=0;
int mid=0;
int high=arr.length-1;
while (mid<=high){
if(arr[mid]==0){
int temp=arr[low];
arr[low]=arr[mid];
arr[mid]=temp;
low++;
mid++;
}else if(arr[mid]==1){
mid++;
}else{
int temp=arr[mid];
arr[mid]=arr[high];
arr[high]=temp;
high--;
}
}


System.out.println(Arrays.toString(arr));

}




}
169 changes: 169 additions & 0 deletions LeetCode/Sortings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package LEasy;

import java.util.ArrayList;
import java.util.Arrays;

public class Sortings {
public static void main(String[] args) {
int[] arr={ 5,4,3,2,1};
int n=arr.length;
// sorting(arr,n);

// bubblesort(arr,n);
// insertionsort(arr,n);
// mergesort(arr,0,n-1);
quicksort(arr,0,n-1);
System.out.println(Arrays.toString(arr));
// System.out.println(Arrays.toString(arr));



}

private static void quicksort(int[] arr, int low, int high) {

if(low<high){
int pidx=partition(arr,low,high);
quicksort(arr,low,pidx-1);
quicksort(arr,pidx+1,high);
}
}

private static int partition(int[] arr, int low, int high) {

int pivot=arr[low];
int i=low;
int j=high;

while (i<j){
while (arr[i]<=pivot && i<=high-1){
i++;
}

while (arr[j]>pivot && j>=low+1){
j--;
}
if(i<j){
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}

}
int temp=arr[low];
arr[low]=arr[j];
arr[j]=temp;

return j;

}

private static void mergesort(int[] arr, int low,int high) {
if(low>=high){
return;
}
// int mid=(low+high)/2;
int mid=low+(high-low)/2;
mergesort(arr,low,mid);
mergesort(arr,mid+1,high);
merge(arr,low,mid,high);






}


private static void merge(int[] arr, int low, int mid, int high) {

ArrayList<Integer> temp=new ArrayList<>();
int left=low;
int right=mid+1;

while (left<=mid && right<=high){

if (arr[left]<=arr[right]){

temp.add(arr[left]);
left++;

}else{
temp.add(arr[right]);
right++;
}
}


while (left<=mid){
temp.add(arr[left]);
left++;
}

while (right<=high){
temp.add(arr[right]);
right++;
}


for (int i = low; i <= high; i++) {
arr[i] = temp.get(i - low);
}

}

private static void insertionsort(int[] arr, int n) {
for (int i = 0; i <=n-1; i++) {
int j=i;
while (j>0 && arr[j-1]>arr[j]){
int temp=arr[j-1];
arr[j-1]=arr[j];
arr[j]=temp;
j--;
}

}

}

private static void bubblesort(int[] arr, int n) {

boolean swapped=false;
for (int i = 0; i < n-1; i++) {

swapped=false;
for (int j = 0; j < n-i-1; j++) {
if(arr[j]>arr[j+1]){
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
swapped=true;
}

}
if(swapped==false){
break;
}



}

}

private static void sorting(int[] arr, int n) {
for (int i = 0; i < n-1; i++) {

int min=i;
for (int j = i+1; j < n; j++) {
if(arr[j]<arr[min]){
min=j;
}
}
int temp=arr[min];
arr[min]=arr[i];
arr[i]=temp;
}
}
}
68 changes: 68 additions & 0 deletions LeetCode/SpiralMatrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package LEasy;

public class SpiralMatrix {
public static void main(String[] args) {
int[][] arr={
{1,2,3,4,5,6},
{20,21,22,23,24,7},
{19,32,33,34,25,8},
{18,31,36,35,26,9},
{17,30,29,28,27,10},
{16,15,14,13,12,11},
};

int n=arr.length;//rows
int m=arr[0].length;//cols



int left=0,right=m-1, top=0,bottom=n-1;

while (top<=bottom && left<=right){


for (int i = left; i <=right ; i++) {
System.out.print(arr[top][i]+" ");
}
top++;

for (int i = top; i <=bottom ; i++) {
System.out.print(arr[i][right]+" ");
}

right--;


for (int i = right; i >=left ; i--) {
System.out.print(arr[bottom][i]+" ");
}
bottom--;


for (int i = bottom; i >=top ; i--) {
System.out.print(arr[i][left]+" ");
}
left++;

}



















}
}