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

added some changes #538

Merged
merged 7 commits into from
Oct 4, 2021
Merged
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
3 changes: 3 additions & 0 deletions Contributors.html
Original file line number Diff line number Diff line change
Expand Up @@ -600,13 +600,16 @@
<a class="box-item" href="https://github.com/amazing-AK"><span>Aditya Krishna</span></a>
<a class="box-item" href="https://github.com/yashjain1974"><span>Yash jain</span></a>
<a class="box-item" href="https://github.com/Ttecs"><span>Tharaka Bandara</span></a>

<a class="box-item" href="https://github.com/Krishnapro"><span>Krishna Kumar</span> </a>

<a class="box-item" href="https://github.com/winxter17"><span>Sharad Kumar</span></a>
<a class="box-item" href="https://github.com/SaranyaSkumar"><span>Saranya S kumar</span></a>
<a class="box-item" href="https://github.com/zaabdn"><span>Zainal Abidin</span></a>
<a class="box-item" href="https://github.com/Krishnapal-rajput"><span>Krishnapal Rajput</span></a>
<a class="box-item" href="https://github.com/SamirKaushik"><span>Samir Kaushik</span></a>
<a class="box-item" href="https://github.com/Aman1905"><span>Aman Chopra</span></a>




Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@


import java.util.Arrays;

public class BubbleSort {

public static void main(String[] args) {
int[] arr = { 5, 4, 3, 2, 1 };
bubble(arr);
System.out.println(Arrays.toString(arr));

}

static void bubble(int[] arr) {

boolean swapped;
// run the steps N-1 times

for (int i = 0; i < arr.length; i++) {
swapped = false;
// for each step, max item will come at last respective index

for (int j = 1; j < arr.length - i; j++) {
// swap if the items are smaller than previous item

if (arr[j] < arr[j - 1]) {
int temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
swapped = true;
}
}

// if you did not swap for a particular value of i, it means array is sorted hence stop program

if(!swapped)
break;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@

import java.util.*;

public class CyclicSort {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int[] arr = {3, 5, 2, 1, 4};
// always remember it should be continous

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

}

// Algo :-
// 1. Start looping from i
// 2. Put it at corrct index
// 3. Only move i when the element at i is sorted

static void sorting(int[] arr) {
int i = 0;
while (i < arr.length) {
int correctInd = arr[i] - 1;
// this should be the correct index

if (arr[i] != arr[correctInd]) {
swap(arr, i, correctInd);
} else {
i++;
}
}
}

static void swap(int[] arr, int first, int second) {
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import java.util.*;

public class InsertionSort {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int[] arr = { 5, 3, 4, 1, 2 };
insertionSorting(arr);
System.out.println(Arrays.toString(arr));

}

static void insertionSorting(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
swapArray(arr, j, j - 1);
} else {
break;
}
}
}
}

static void swapArray(int[] arr, int first, int second) {
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

import java.util.*;

public class SelectionSort {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int[] arr = { 4, 6, 7, 2, 1, 5, 3, 8, 9 };
selectionSorting(arr);
System.out.println(Arrays.toString(arr));

}

static void selectionSorting(int[] arr) {
for (int i = 0; i < arr.length; i++) {
// find the max item in the remaining array and swap with correct index

int last = arr.length - i - 1;

int maxInd = getMaxInd(arr, 0, last);
swapArray(arr, maxInd, last);
}
}

private static int getMaxInd(int[] arr, int start, int end) {
int max = start;

for (int i = start; i <= end; i++) {
if (arr[max] < arr[i])
max = i;
}
return max;
}

static void swapArray(int[] arr, int first, int second) {
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}

}