Skip to content

Commit 51546fa

Browse files
Sorting-Algo
1 parent 610a49a commit 51546fa

20 files changed

+573
-2
lines changed

Java/2D.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Sum of rows,cols,diagonal
2+
3+
class 2D{
4+
public static void main(String s[]){
5+
6+
int a[][] = new int[5][5];
7+
for(int i=0;i<n;i++){
8+
for(int j=0;j<n;j++){
9+
a[i][j] = i*j+1;
10+
}
11+
}
12+
13+
// Sum of rows
14+
int rsum=0,csum=0,dsum=0;
15+
for(int i=0;i<5;i++){
16+
rsum=0;
17+
for(int j=0;j<n;j++){
18+
rsum += a[i][j];
19+
}
20+
System.out.println("Sum of row "+i+" = "+rsum);
21+
}
22+
23+
// Sum of cols
24+
25+
26+
27+
// Sum of diagonal
28+
29+
30+
}
31+
}

Java/LinkedListImplementation.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Node{
2+
int a ;
3+
Node next;
4+
5+
Node(int x,Node z){
6+
a=x;
7+
next = z;
8+
}
9+
10+
}
11+
12+
class LinkedList{
13+
Node head;
14+
Node append(int data){
15+
Node n = new Node(data,null);
16+
if(head==null){
17+
head = n;
18+
}
19+
else{
20+
Node temp = head;
21+
while(temp.next!=null){
22+
temp = temp.next;
23+
24+
}
25+
temp.next = n;
26+
}
27+
return head;
28+
}
29+
30+
Node appendStart(int data,Node head){
31+
32+
Node head2 = new Node(data,head);
33+
return head2;
34+
}
35+
36+
void print(Node head){
37+
Node temp = head;
38+
while(temp!=null){
39+
System.out.println(temp.a+" ");
40+
temp = temp.next;
41+
}
42+
}
43+
}
44+
45+
class LinkedListImplementation{
46+
public static void main(String s[]){
47+
LinkedList ll = new LinkedList();
48+
Node head = ll.append(10);
49+
head = ll.append(20);
50+
head = ll.append(30);
51+
head = ll.append(40);
52+
head = ll.append(50);
53+
54+
ll.print(head);
55+
56+
Node head2 = ll.appendStart(0,head);
57+
58+
ll.print(head2);
59+
}
60+
}

Java/Maximum.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.*;
2+
class Maximum{
3+
static Scanner sc = new Scanner(System.in);
4+
5+
public static void main(String s[]){
6+
7+
int n = sc.nextInt();
8+
int a[] = new int[n];
9+
for(int i=0;i<n;i++){
10+
a[i] = sc.nextInt();
11+
}
12+
int sum = find(n,a);
13+
System.out.println(sum);
14+
}
15+
public static int find(int n, int[] a){
16+
int sum = a[0];
17+
for(int i=1;i<a.length;i++){
18+
if(a[i] > sum)
19+
sum = a[i];
20+
}
21+
return sum;
22+
}
23+
}

Java/Modification.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Modification{
2+
public static void main(String s[]){
3+
/*int a = 5;
4+
modify(a);
5+
System.out.println(a);
6+
*/
7+
8+
Balloon b = new Balloon("red");
9+
modify(b);
10+
System.out.println(b.color);
11+
}
12+
13+
public static void modify(Balloon c){
14+
c.color="grey";
15+
//c = new Balloon("grey");
16+
}
17+
}
18+
19+
class Balloon{
20+
21+
String color="blue";
22+
Balloon(String y){
23+
color=y;
24+
}
25+
}

Java/Selection.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.*;
2+
class Selection{
3+
public static void main(String s[]){
4+
Scanner sc = new Scanner(System.in);
5+
int n = sc.nextInt();
6+
int a[] = new int[n];
7+
for(int i=0;i<n;i++)
8+
a[i] = sc.nextInt();
9+
10+
int i=0,max,t=0,current=0;
11+
//for(i=0;i<n;i++){
12+
for(int j=i+1;j<n;j++){
13+
max = findMax(i,a);
14+
t = a[max];
15+
a[max] = a[i];
16+
a[i] = t;
17+
i++;
18+
}
19+
//a[i] = max;
20+
//}
21+
22+
for( i=0;i<n;i++)
23+
System.out.print(a[i]+" ");
24+
}
25+
26+
public static int findMax(int current,int a[]){
27+
for(int i=current+1;i<a.length;i++){
28+
if(a[i]>a[current])
29+
current = i;
30+
}
31+
return current;
32+
}
33+
}

Java/StudentDemo.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
class Student{
2+
3+
static int no_of_student=0;
4+
5+
String name;
6+
int grade=0;
7+
int marks;
8+
9+
/*void get(String a , String b , int c){
10+
name = a;
11+
grade = b;
12+
marks = c;
13+
} */
14+
15+
Student(){
16+
no_of_student++;
17+
}
18+
19+
void print(){
20+
System.out.println("Grade = "+grade);
21+
}
22+
23+
void clearSemester(){
24+
grade += 1;
25+
}
26+
27+
static void decrementCount(){
28+
no_of_student--;
29+
}
30+
31+
}
32+
33+
class StudentDemo{
34+
public static void main(String s[]){
35+
Student s1 = new Student();
36+
Student s2 = new Student();
37+
Student s3 = new Student();
38+
Student s4 = new Student();
39+
Student s5 = new Student();
40+
41+
s1.clearSemester();
42+
s1.print();
43+
s2.clearSemester();
44+
s2.print();
45+
s3.clearSemester();
46+
s3.print();
47+
s4.clearSemester();
48+
s4.print();
49+
s5.clearSemester();
50+
s5.print();
51+
52+
s1.clearSemester();
53+
s1.print();
54+
55+
System.out.println("No of Student = "+Student.no_of_student);
56+
Student.decrementCount();
57+
System.out.println("No of Student = "+Student.no_of_student);
58+
59+
}
60+
}

Java/array.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import java.util.*;
2+
class array{
3+
public static void main(String s[]){
4+
int[] a = new int[100];
5+
modify(a);
6+
Scanner sc = new Scanner(System.in);
7+
System.out.println(sc);
8+
System.out.println(a[0]); //Passed value of reference;
9+
}
10+
public static void modify(int[] a){
11+
a[0] = 10;
12+
}
13+
}

Java/binary_search.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Binary Search
2+
3+
class binary_search{
4+
public static void main(String s[]){
5+
int a[] = {1,5,7,8,13,15,17};
6+
int x = 8;
7+
8+
int start = 0,end = 7;
9+
10+
for(start=0;start<end;start++){
11+
int mid = (start+end)/2;
12+
13+
if(a[mid]==x){
14+
System.out.println("Found");
15+
break;
16+
}
17+
if(a[mid]>x){
18+
end = mid;
19+
}
20+
if(a[mid]<x){
21+
start = mid;
22+
}
23+
24+
}
25+
}
26+
}

Java/comparison.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class comparison{
2+
3+
public static void main(String s[]){
4+
String s1 = new String("DEF");
5+
String s2 = new String("DEF");
6+
String s3 = "xyz";
7+
String s4 = "xyz";
8+
System.out.println(s3==s4);
9+
System.out.println(s3.equals(s4));
10+
System.out.println(s1==s2);
11+
System.out.println(s1.equals(s2));
12+
}
13+
}

Java/inheritance.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class inheritance{
2+
public static void main (String s[]){
3+
Child c = new Child();
4+
Parent p = new Child();
5+
//Child c2 = new Parent();
6+
//c2.method1();
7+
//c.method1();
8+
p.method1();
9+
}
10+
}
11+
class Parent{
12+
void method1(){
13+
System.out.println("Hey! I'm in parent");
14+
}
15+
}
16+
17+
class Child extends Parent{
18+
void method1(){
19+
System.out.println("Hey! im in child");
20+
}
21+
}

Java/memorization.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.*;
2+
class memorization{
3+
public static void main(String s[]){
4+
Scanner sc = new Scanner(System.in);
5+
int test = sc.nextInt();
6+
int input[] = new int[test];
7+
8+
for(int i=0;i<test;i++){
9+
input[i] = sc.nextInt();
10+
}
11+
int max = Maximum.find(test,input);
12+
13+
System.out.println("Maximum no = "+max);
14+
15+
int f[] = new int[test+1];
16+
f = factorial(max);
17+
18+
for(int i=0;i<test;i++){
19+
System.out.println(f[input[i]]);
20+
}
21+
22+
}
23+
public static int[] factorial(int n){
24+
int factorials[] = new int[n+1];
25+
factorials[0] = 1;
26+
int fact=1;
27+
for(int i=1;i<=n;i++){
28+
29+
factorials[i] =( i * factorials[i-1])%10000007;
30+
}
31+
return factorials;
32+
}
33+
}

Java/recur.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.*;
2+
class recur{
3+
public static void main(String s[]){
4+
Scanner sc = new Scanner(System.in);
5+
int no = sc.nextInt();
6+
System.out.println(fact(1,no));
7+
System.out.println(iter(no));
8+
}
9+
public static int fact(int n,int no){
10+
if(n==no)
11+
return n;
12+
else
13+
return n*fact(n+1,no);
14+
}
15+
public static int iter(int no){
16+
int fact=1;
17+
for(int i=2;i<=no;i++){
18+
fact = fact *i;
19+
}
20+
return fact;
21+
}
22+
}

Python/Binary-Insertion-Sort.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
print('****** Binary Insertion Sort **********')
2+
3+
# Used to reduced the number of comparison in Insertion Sort O(log n) but shifting the elements after insertion takes O(n)
4+
# Complexity : O(nlogn)-> Comparisons and O(n*n) swaps

0 commit comments

Comments
 (0)