Skip to content

Commit fc9d160

Browse files
Add files via upload
1 parent 268bb04 commit fc9d160

File tree

8 files changed

+343
-0
lines changed

8 files changed

+343
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.company;
2+
3+
import java.util.Scanner;
4+
5+
public class BinarySearch {
6+
public static void main(String[] args) {
7+
Scanner sc=new Scanner(System.in);
8+
System.out.println("Enter the array size");
9+
int n=sc.nextInt();
10+
int a[]=new int[n];
11+
System.out.println("Enter the numbers");
12+
for(int i=0;i<n;i++) {
13+
a[i] = sc.nextInt();
14+
}
15+
System.out.println("Enter the search number");
16+
int searchnumber=sc.nextInt();
17+
if(binarysearch(a,searchnumber)!=-1)
18+
System.out.println("The number "+ binarysearch(a,searchnumber)+" is present in the array");
19+
else
20+
System.out.println("The number is not present in the array");
21+
}
22+
public static int binarysearch(int[] array,int number){
23+
int low=0,mid=0;
24+
int high=array.length-1;
25+
while(low<=high){
26+
mid=(high+low)/2;
27+
if(mid==number)
28+
return mid;
29+
if(number<mid)
30+
high=mid-1;
31+
else if(number>mid)
32+
low=mid+1;
33+
}
34+
return -1;
35+
}
36+
37+
38+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.Scanner;
2+
3+
public class InsertionSort {
4+
public static Scanner scanner=new Scanner(System.in);
5+
public static void main(String args[]) {
6+
System.out.println("Enter the number of inputs");
7+
int n=scanner.nextInt();
8+
int[] myIntegers=getIntegers(n);
9+
sort(myIntegers);
10+
for(int i=0;i<myIntegers.length;i++){
11+
System.out.println(myIntegers[i]);
12+
}
13+
}
14+
15+
public static int[] getIntegers(int number){
16+
System.out.println("Enter the values");
17+
int[] values=new int[number];
18+
for(int i=0;i<number;i++){
19+
values[i]=scanner.nextInt();
20+
}
21+
return values;
22+
}
23+
public static void sort(int[] array){
24+
for(int i=1;i<array.length;i++){
25+
for(int j=i;j>0;j--){
26+
if(array[j]<array[j-1]){
27+
int temp=array[j-1];
28+
array[j-1]=array[j];
29+
array[j]=temp;
30+
}
31+
}
32+
}
33+
}
34+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.company;
2+
3+
public class SortingLinkedList {
4+
static class Node {
5+
int data;
6+
Node next;
7+
}
8+
Node head;
9+
public int size(){
10+
int count=0;
11+
if(head==null){
12+
count=0;
13+
}else{
14+
Node n=head;
15+
while(n!=null){
16+
count++;
17+
n=n.next;
18+
}
19+
}
20+
return count;
21+
}
22+
public void insert(int data){
23+
Node node=new Node();
24+
node.data=data;
25+
node.next=null;
26+
if(head==null){
27+
head=node;
28+
}else{
29+
Node n=head;
30+
while(n.next!=null){
31+
n=n.next;
32+
}
33+
n.next=node;
34+
}
35+
}
36+
public void show(){
37+
Node node=head;
38+
while(node!=null){
39+
System.out.println(node.data);
40+
node=node.next;
41+
}
42+
}
43+
public void sort(){
44+
head=mergeSort(head);
45+
}
46+
private Node mergeSort(Node head){
47+
if(head==null || head.next==null){
48+
return head;
49+
}
50+
Node temp=head;
51+
Node slow=head;
52+
Node fast=head;
53+
while(fast!=null && fast.next!=null){
54+
temp=slow;
55+
slow=slow.next;
56+
fast=fast.next.next;
57+
}
58+
temp.next=null;
59+
Node left=mergeSort(head);
60+
Node right=mergeSort(slow);
61+
return merge(left,right);
62+
}
63+
private Node merge(Node a,Node b){
64+
Node result=null;
65+
if(a==null){
66+
return b;
67+
}
68+
if(b==null){
69+
return a;
70+
}
71+
if(a.data<=b.data){
72+
result=a;
73+
result.next=merge(a.next,b);
74+
}else{
75+
result=b;
76+
result.next=merge(a,b.next);
77+
}
78+
return result;
79+
}
80+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.company;
2+
3+
public class MergeSort {
4+
void sort(int arr[],int beg,int end) {
5+
if(beg < end) {
6+
int mid = (beg + end) / 2;
7+
sort(arr, beg, mid);
8+
sort(arr, mid + 1, end);
9+
mergesort(arr, beg, mid, end);
10+
}
11+
}
12+
void mergesort(int arr[],int beg,int mid,int end){
13+
int left=mid-beg+1;
14+
int right=end-mid;
15+
int[] leftarray=new int[left];
16+
int[] rightarray=new int[right];
17+
for(int i=0;i<left;i++){
18+
leftarray[i]=arr[beg+i];
19+
}
20+
for(int i=0;i<right;i++){
21+
rightarray[i]=arr[mid+i+1];
22+
}
23+
int i=0,j=0,k=beg;
24+
while(i<left&&j<right){
25+
if(leftarray[i]<rightarray[j])
26+
arr[k++]=leftarray[i++];
27+
else
28+
arr[k++]=rightarray[j++];
29+
}
30+
while(i<left){
31+
arr[k++]=leftarray[i++];
32+
}
33+
while(j<right){
34+
arr[k++]=rightarray[j++];
35+
}
36+
}
37+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.company;
2+
3+
public class MergeTwoLinkedList {
4+
class Node {
5+
int data;
6+
Node next;
7+
}
8+
Node head;
9+
public int size(){
10+
int count=0;
11+
if(head==null){
12+
count=0;
13+
}else{
14+
Node n=head;
15+
while(n!=null){
16+
count++;
17+
n=n.next;
18+
}
19+
}
20+
return count;
21+
}
22+
public void insert(int data){
23+
Node node=new Node();
24+
node.data=data;
25+
node.next=null;
26+
if(head==null){
27+
head=node;
28+
}else{
29+
Node n=head;
30+
while(n.next!=null){
31+
n=n.next;
32+
}
33+
n.next=node;
34+
}
35+
}
36+
public void show(){
37+
Node node=head;
38+
while(node!=null){
39+
System.out.println(node.data);
40+
node=node.next;
41+
}
42+
}
43+
public void merge(MergeTwoLinkedList m1,MergeTwoLinkedList m2){
44+
m1.head=mergeSortedLinkedList(m1.head,m2.head);
45+
}
46+
public Node mergeSortedLinkedList(Node a,Node b){
47+
Node dummyHead=new Node();
48+
Node tail=dummyHead;
49+
while(true){
50+
if(a==null) {
51+
tail.next = b;
52+
break;
53+
}
54+
if(b==null) {
55+
tail.next = a;
56+
break;
57+
}
58+
if(a.data<b.data){
59+
tail.next=a;
60+
a=a.next;
61+
}else{
62+
tail.next=b;
63+
b=b.next;
64+
}
65+
tail=tail.next;
66+
}
67+
return dummyHead.next;
68+
}
69+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.company;
2+
public class Test {
3+
public static void main (String[] args) {
4+
MergeTwoLinkedList mll1 = new MergeTwoLinkedList();
5+
mll1.insert(2);
6+
mll1.insert(4);
7+
MergeTwoLinkedList mll2 = new MergeTwoLinkedList();
8+
mll2.insert(3);
9+
mll2.insert(5);
10+
mll1.merge(mll1,mll2);
11+
mll1.show();
12+
}
13+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class QSort{
2+
public int partition(int[] array,int l,int h){
3+
int i=l;
4+
int pivot=array[h];
5+
for(int j=l;j<h;j++){
6+
if(array[j]<=pivot){
7+
int temp=array[i];
8+
array[i]=array[j];
9+
array[j]=temp;
10+
i++;
11+
}
12+
}
13+
int temp=array[i];
14+
array[i]=array[h];
15+
array[h]=temp;
16+
return i;
17+
}
18+
public void sort(int[] array,int l,int h){
19+
if(l<h){
20+
int p=partition(array,l,h);
21+
sort(array,l,p-1);
22+
sort(array,p+1,h);
23+
}
24+
}
25+
}
26+
public class QuickSort {
27+
public static void main (String[] args) {
28+
int[] array={10, 7, 8, 9, 1, 5,6,34,521,6,31,35216,621,62,2136,4126,62};
29+
int n=array.length;
30+
QSort qs=new QSort();
31+
qs.sort(array,0,n-1);
32+
for(int i=0;i<n;i++){
33+
System.out.println(array[i]);
34+
}
35+
}
36+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import java.util.Scanner;
2+
3+
public class SelectionSort {
4+
public static Scanner scanner=new Scanner(System.in);
5+
public static void main(String args[]) {
6+
System.out.println("Enter the number of inputs");
7+
int n=scanner.nextInt();
8+
int[] myIntegers=getIntegers(n);
9+
sort(myIntegers);
10+
for(int i=0;i<myIntegers.length;i++){
11+
System.out.println(myIntegers[i]);
12+
}
13+
}
14+
15+
public static int[] getIntegers(int number){
16+
System.out.println("Enter the values");
17+
int[] values=new int[number];
18+
for(int i=0;i<number;i++){
19+
values[i]=scanner.nextInt();
20+
}
21+
return values;
22+
}
23+
public static void sort(int[] array){
24+
for(int i=0;i<array.length;i++){
25+
int min=i,temp=0;
26+
for(int j=i+1;j<array.length;j++){
27+
if(array[j]<array[min]){
28+
min=j;
29+
}
30+
}
31+
temp=array[i];
32+
array[i]=array[min];
33+
array[min]=temp;
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)