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

Created sorting folder #37

Closed
wants to merge 1 commit into from
Closed
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
Binary file added sorting/bubblesort.class
Binary file not shown.
44 changes: 44 additions & 0 deletions sorting/bubblesort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.util.Scanner;
public class bubblesort{


static void printarray(int [] array){
for(int i = 0;i<array.length;i++){
System.out.print(array[i]+"\t");
}
}

static void inputarray(int [] array){
Scanner sc = new Scanner(System.in);
for(int i=0; i<array.length; i++){
array[i] = sc.nextInt();

}
}

static void Bubblesort(int [] array){
for(int i=0;i<array.length-1;i++){
for(int j=0;j<array.length-i-1;j++){
if(array[j]>array[j+1]){
int temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
}
}
printarray(array);
}


public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array --->");
int n = sc.nextInt();
int array [] = new int[n];
System.out.println("Enter the array --->");
inputarray(array);
Bubblesort(array);
sc.close();

}
}
Binary file added sorting/insertionsort.class
Binary file not shown.
43 changes: 43 additions & 0 deletions sorting/insertionsort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.util.Scanner;
public class insertionsort {
static void printarray(int [] array){
for(int i = 0;i<array.length;i++){
System.out.print(array[i]+"\t");
}
}

static void inputarray(int [] array){
Scanner sc = new Scanner(System.in);
for(int i=0; i<array.length; i++){
array[i] = sc.nextInt();

}
}


static void Insertionsort(int [] array){
for (int i = 1; i < array.length; i++) {
int current = array[i];
int j = i-1;
while(j>=0 && current<array[j]){
array[j] = array[j+1];
j--;
}
array[j+1] = current;
}
printarray(array);
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array --->");
int n = sc.nextInt();
int array [] = new int[n];
System.out.println("Enter the array --->");
inputarray(array);
Insertionsort(array);
sc.close();

}
}

Binary file added sorting/quicksort.class
Binary file not shown.
72 changes: 72 additions & 0 deletions sorting/quicksort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import java.util.Scanner;

public class quicksort
{

int partition (int a[], int start, int end)
{
int pivot = a[end];
int i = (start - 1);


for (int j = start; j <= end - 1; j++)
{

if (a[j] < pivot)
{
i++;
int t = a[i];
a[i] = a[j];
a[j] = t;
}
}
int t = a[i+1];
a[i+1] = a[end];
a[end] = t;
return (i + 1);
}



static void inputarray(int [] array){
Scanner sc = new Scanner(System.in);
for(int i=0; i<array.length; i++){
array[i] = sc.nextInt();
}
}

void quick(int a[], int start, int end)
{
if (start < end)
{
int p = partition(a, start, end);
quick(a, start, p - 1);
quick(a, p + 1, end);
}
}


void printArr(int a[], int n)
{
int i;
for (i = 0; i < n; i++)
System.out.print(a[i] + " ");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the length of array: ");
int m = sc.nextInt();
int a [] = new int[m];
System.out.println("Enter the array: ");
inputarray(a);
int n = a.length;
System.out.println("\nBefore sorting array elements are - ");
quicksort q1 = new quicksort();
q1.printArr(a, n);
q1.quick(a, 0, n - 1);
System.out.println("\nAfter sorting array elements are - ");
q1.printArr(a, n);
System.out.println();
sc.close();
}
}
Binary file added sorting/selectionsort.class
Binary file not shown.
47 changes: 47 additions & 0 deletions sorting/selectionsort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import java.util.Scanner;
public class selectionsort {
static void printarray(int [] array){
for(int i = 0;i<array.length;i++){
System.out.print(array[i]+"\t");
}
}

static void inputarray(int [] array){
Scanner sc = new Scanner(System.in);
for(int i=0; i<array.length; i++){
array[i] = sc.nextInt();

}
}


static void Selectionsort(int [] array){
for(int i=0;i<array.length-1;i++){
int smallest = i;
for(int j=i+1;j<array.length;j++){
if(array[smallest]>array[j]){
smallest = j;
}
}
int temp = array[smallest];
array[smallest]= array[i];
array[i] = temp;

}
printarray(array);
}



public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of array --->");
int n = sc.nextInt();
int array [] = new int[n];
System.out.println("Enter the array --->");
inputarray(array);
Selectionsort(array);
sc.close();
}
}