Skip to content

Commit 9b52615

Browse files
committed
First commit for Data_Structure_In_Java
0 parents  commit 9b52615

7 files changed

Lines changed: 274 additions & 0 deletions

File tree

Data_Structure/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
4+
<classpathentry kind="src" path="src"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

Data_Structure/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

Data_Structure/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>Data_Structure</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.8
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.8
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package chap3_Simple_Sorting;
2+
3+
import java.util.Date;
4+
5+
class ArrayBub{
6+
private long[] a;
7+
private int nElems; //the number of array elements
8+
public int num = 0;
9+
10+
public ArrayBub(int Max) { //constructor
11+
a = new long[Max];
12+
nElems = 0;
13+
}
14+
15+
public void insert(long value) {
16+
a[nElems] = value;
17+
nElems++;
18+
}
19+
20+
public void display() {
21+
for(int i = 0; i < nElems; i++ ) {
22+
System.out.println(a[i] + " ");
23+
num ++;
24+
}
25+
System.out.println();
26+
}
27+
28+
public void bubbleSort() {
29+
for(int out = nElems -1; out > 1; out--) {
30+
for(int in = 0; in < out; in++) {
31+
if(a[in] > a[in+1]) {
32+
swap(in, in+1);
33+
}
34+
}
35+
}
36+
}
37+
38+
private void swap(int one, int two) {
39+
long temp = a[one];
40+
a[one] = a[two];
41+
a[two] = temp;
42+
}
43+
44+
}
45+
46+
public class BubbleSortApp {
47+
48+
public static void main(String[] args) {
49+
// TODO Auto-generated method stub
50+
int maxSize =50000;
51+
long n;
52+
53+
ArrayBub arr = new ArrayBub(maxSize);
54+
55+
for (int i = 0; i < maxSize; i++) {
56+
n = (long)(Math.random() * maxSize);
57+
arr.insert(n);
58+
}
59+
arr.display();
60+
arr.bubbleSort();
61+
System.out.println(arr.num);
62+
arr.display();
63+
// int maxSize = 100;
64+
// ArrayBub arr = new ArrayBub(maxSize);
65+
//
66+
// arr.insert(88);
67+
// arr.insert(23);
68+
// arr.insert(18);
69+
// arr.insert(67);
70+
// arr.insert(43);
71+
// arr.insert(48);
72+
// arr.insert(0);
73+
// arr.insert(99);
74+
// arr.insert(75);
75+
// arr.insert(44);
76+
// arr.insert(12);
77+
//
78+
// System.out.println("Before bubblesort:");
79+
// arr.display();
80+
//
81+
// arr.bubbleSort();
82+
// System.out.println("After bubblesort:");
83+
// arr.display();
84+
85+
}
86+
87+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package chap3_Simple_Sorting;
2+
3+
4+
class ArrayIns{
5+
private long[] a;
6+
private int nElems; //the number of array elements
7+
8+
public ArrayIns(int Max) { //constructor
9+
a = new long[Max];
10+
nElems = 0;
11+
}
12+
13+
public void insert(long value) {
14+
a[nElems] = value;
15+
nElems++;
16+
}
17+
18+
public void display() {
19+
for(int i = 0; i < nElems; i++ ) {
20+
System.out.print(a[i] + " ");
21+
}
22+
System.out.println();
23+
}
24+
25+
public void insertSort() {
26+
int in;
27+
long temp;
28+
for(int out = 1; out < nElems ; out++) {
29+
temp = a[out];
30+
for( in = out -1; in >=0; in--) {
31+
32+
if(a[in] > temp) {
33+
a[in+1] = a[in];
34+
}else {
35+
break;
36+
}
37+
}
38+
a[in+1] = temp;
39+
}
40+
}
41+
42+
private void swap(int one, int two) {
43+
long temp = a[one];
44+
a[one] = a[two];
45+
a[two] = temp;
46+
}
47+
48+
}
49+
50+
public class InsertSortApp {
51+
52+
public static void main(String[] args) {
53+
// TODO Auto-generated method stub
54+
int maxSize = 100;
55+
ArrayIns arr = new ArrayIns(maxSize);
56+
57+
arr.insert(88);
58+
arr.insert(23);
59+
arr.insert(18);
60+
arr.insert(67);
61+
arr.insert(43);
62+
arr.insert(48);
63+
arr.insert(0);
64+
arr.insert(99);
65+
arr.insert(75);
66+
arr.insert(44);
67+
arr.insert(12);
68+
69+
System.out.println("Before insertsort:");
70+
arr.display();
71+
72+
arr.insertSort();
73+
System.out.println("After insertsort:");
74+
arr.display();
75+
76+
}
77+
78+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package chap3_Simple_Sorting;
2+
3+
4+
class ArraySel{
5+
private long[] a;
6+
private int nElems; //the number of array elements
7+
8+
public ArraySel(int Max) { //constructor
9+
a = new long[Max];
10+
nElems = 0;
11+
}
12+
13+
public void insert(long value) {
14+
a[nElems] = value;
15+
nElems++;
16+
}
17+
18+
public void display() {
19+
for(int i = 0; i < nElems; i++ ) {
20+
System.out.print(a[i] + " ");
21+
}
22+
System.out.println();
23+
}
24+
25+
public void selectSort() {
26+
int min = 0;
27+
for(int out = 0; out < nElems -1; out++) {
28+
min = out;
29+
for(int in = out +1; in < nElems; in++) {
30+
if(a[in] < a[min]) {
31+
min = in;
32+
}
33+
}
34+
swap(min, out);
35+
}
36+
}
37+
38+
private void swap(int one, int two) {
39+
long temp = a[one];
40+
a[one] = a[two];
41+
a[two] = temp;
42+
}
43+
44+
}
45+
46+
public class SelectSortApp {
47+
48+
public static void main(String[] args) {
49+
// TODO Auto-generated method stub
50+
int maxSize = 100;
51+
ArraySel arr = new ArraySel(maxSize);
52+
53+
arr.insert(88);
54+
arr.insert(23);
55+
arr.insert(18);
56+
arr.insert(67);
57+
arr.insert(43);
58+
arr.insert(48);
59+
arr.insert(0);
60+
arr.insert(99);
61+
arr.insert(75);
62+
arr.insert(44);
63+
arr.insert(12);
64+
65+
System.out.println("Before selectsort:");
66+
arr.display();
67+
68+
arr.selectSort();
69+
System.out.println("After selectsort:");
70+
arr.display();
71+
72+
}
73+
74+
}

0 commit comments

Comments
 (0)