Skip to content

Commit 3fef714

Browse files
author
phyex0
committed
Code has been reformatted.
1 parent 7146741 commit 3fef714

7 files changed

Lines changed: 165 additions & 190 deletions

File tree

src/com/company/Algorithms/BinarySearch.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
package com.company.Algorithms;
22

33
/*
4-
* Binary Search:
5-
* Best case = o(1)
6-
* Average case = o(log(n))
7-
* Worst case = o(log(n))
8-
*
9-
*
10-
* Making a search on sorted array.
11-
* */
4+
* Binary Search:
5+
* Best case = o(1)
6+
* Average case = o(log(n))
7+
* Worst case = o(log(n))
8+
*
9+
*
10+
* Making a search on sorted array.
11+
* */
1212

1313

1414
public class BinarySearch {
1515

16-
public static int binarySearch(int[] arr,int val){
17-
int index=-1, left = 0, right=arr.length, mid;
16+
public static int binarySearch(int[] arr, int val) {
17+
int index = -1, left = 0, right = arr.length, mid;
1818

19-
while(right>1){
20-
mid = 1+(left-right)/2;
19+
while (right > 1) {
20+
mid = 1 + (left - right) / 2;
2121

22-
if(arr[mid]== val)
23-
index= mid;
22+
if (arr[mid] == val)
23+
index = mid;
2424

25-
else if(arr[mid] > val)
25+
else if (arr[mid] > val)
2626
left--;
2727

2828
else

src/com/company/Algorithms/BubbleSort.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
package com.company.Algorithms;
22

33
/*
4-
* Bubble Sort:
5-
* Best case = O(n)
6-
* Average case = O(n^2)
7-
* Worst case = O(n^2)
8-
*
9-
* This algorithm sorts the array min to max. If current item is greater than the next it swaps.
10-
*/
4+
* Bubble Sort:
5+
* Best case = O(n)
6+
* Average case = O(n^2)
7+
* Worst case = O(n^2)
8+
*
9+
* This algorithm sorts the array min to max. If current item is greater than the next it swaps.
10+
*/
1111

1212
public class BubbleSort {
1313

14-
public static void bubbleSort(int[] arr){
14+
public static void bubbleSort(int[] arr) {
1515
int len = arr.length;
1616

17-
for(int i=0;i<len-1;i++){
18-
for(int j=0;j<len-i-1;j++){
19-
if(arr[j]>arr[j+1])
20-
swap(arr,j,j+1);
17+
for (int i = 0; i < len - 1; i++) {
18+
for (int j = 0; j < len - i - 1; j++) {
19+
if (arr[j] > arr[j + 1])
20+
swap(arr, j, j + 1);
2121
}
2222
}
2323

2424
}
2525

2626
//swaps the elements.
27-
private static void swap(int[] arr, int i,int j){
27+
private static void swap(int[] arr, int i, int j) {
2828
int temp = arr[i];
2929
arr[i] = arr[j];
3030
arr[j] = temp;

src/com/company/Algorithms/LinearSearch.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
package com.company.Algorithms;
22

33
/*
4-
* Linear Search Algorithm:
5-
* Best case = O(1)
6-
* Worst case = O(n)
7-
* Average case = O(n)
8-
*
9-
* If the given item is not in your list it returns -1 otherwise it returns the index that was first found.
10-
*/
4+
* Linear Search Algorithm:
5+
* Best case = O(1)
6+
* Worst case = O(n)
7+
* Average case = O(n)
8+
*
9+
* If the given item is not in your list it returns -1 otherwise it returns the index that was first found.
10+
*/
1111
public class LinearSearch {
1212

13-
public static int linearSearch(int[] arr, int val){
14-
int index=-1;
15-
for(int i=0;i<arr.length;i++)
16-
if(arr[i]==val){
17-
index=i;
13+
public static int linearSearch(int[] arr, int val) {
14+
int index = -1;
15+
for (int i = 0; i < arr.length; i++)
16+
if (arr[i] == val) {
17+
index = i;
1818
break;
1919
}
2020

Lines changed: 82 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,181 +1,170 @@
11
package com.company.DataStructures;
22

33
/*
4-
* Linked List:
5-
* Random access is not simple like the arrays
6-
* Linked List has unlimited size. It depending your memory.
7-
*
8-
* Inserting to head = o(1)
9-
* Inserting to end = o(1)
10-
* Inserting to given index = o(n)
11-
* Deleting given item = o(n)
12-
* Reversing the list = o(n)
13-
* Searching an item = o(n)
14-
*
15-
*/
4+
* Linked List:
5+
* Random access is not simple like the arrays
6+
* Linked List has unlimited size. It depending your memory.
7+
*
8+
* Inserting to head = o(1)
9+
* Inserting to end = o(1)
10+
* Inserting to given index = o(n)
11+
* Deleting given item = o(n)
12+
* Reversing the list = o(n)
13+
* Searching an item = o(n)
14+
*
15+
*/
1616

1717
public class LinkedList {
1818

19-
class Node{
20-
int val;
21-
Node next;
22-
23-
24-
public Node(int val) {
25-
this.val = val;
26-
next=null;
27-
}
28-
29-
}
30-
31-
Node root;
32-
Node tail;
33-
19+
Node root;
20+
Node tail;
3421
public LinkedList() {
35-
this.root =null;
22+
this.root = null;
3623
this.tail = null;
3724
}
3825

3926
//inserting data to end of list.
40-
public void insertToEnd(int val){
41-
if(root ==null){
27+
public void insertToEnd(int val) {
28+
if (root == null) {
4229
root = new Node(val);
43-
tail =root;
44-
tail.next=null;
45-
}
46-
47-
else {
48-
tail.next= new Node(val);
49-
tail =tail.next;
50-
tail.next=null;
30+
tail = root;
31+
tail.next = null;
32+
} else {
33+
tail.next = new Node(val);
34+
tail = tail.next;
35+
tail.next = null;
5136

5237
}
5338
}
5439

5540
//inserting data to head of list.
56-
public void insertToHead(int val){
41+
public void insertToHead(int val) {
5742
Node temp = new Node(val);
58-
if(root==null){
59-
root=temp;
60-
tail=root;
61-
tail.next=null;
62-
}
63-
64-
else{
65-
temp.next=root;
66-
root=temp;
43+
if (root == null) {
44+
root = temp;
45+
tail = root;
46+
tail.next = null;
47+
} else {
48+
temp.next = root;
49+
root = temp;
6750
}
6851

6952
}
7053

7154
//inserting data to given index;
72-
public void insertToIndex(int index, int val){
55+
public void insertToIndex(int index, int val) {
7356
Node temp = new Node(val);
7457
Node iter = root;
7558
int len = lenOfList();
7659

77-
if(index<0 || index>len)
78-
System.out.println("You cannot insert to given position: "+index);
60+
if (index < 0 || index > len)
61+
System.out.println("You cannot insert to given position: " + index);
7962

80-
else if(index==0)
63+
else if (index == 0)
8164
insertToHead(val);
8265

83-
else if(index<=len) {
84-
for (int i = 0; i < index-1; i++)
66+
else if (index <= len) {
67+
for (int i = 0; i < index - 1; i++)
8568
iter = iter.next;
8669
temp.next = iter.next;
8770
iter.next = temp;
8871
}
8972
}
9073

9174
//returns length of the list.
92-
public int lenOfList(){
75+
public int lenOfList() {
9376
Node iter = root;
94-
int count =0;
95-
while(iter!=null){
77+
int count = 0;
78+
while (iter != null) {
9679
count++;
97-
iter=iter.next;
80+
iter = iter.next;
9881
}
9982
return count;
10083
}
10184

10285
//printing list
103-
public void print(){
86+
public void print() {
10487

105-
Node iter = root;
88+
Node iter = root;
10689

107-
while(iter!=null){
90+
while (iter != null) {
10891
System.out.println(iter.val);
109-
iter=iter.next;
92+
iter = iter.next;
11093
}
11194

11295
}
11396

11497
//deleting item
115-
public void deleteItem(int val){
116-
boolean isHere =false;
98+
public void deleteItem(int val) {
99+
boolean isHere = false;
117100
Node iter = root;
118101
Node temp = null;
119-
if(root.val ==val){
120-
temp=root;
121-
root=root.next;
122-
temp=null;
102+
if (root.val == val) {
103+
temp = root;
104+
root = root.next;
105+
temp = null;
123106
isHere = true;
124107
return;
125108
}
126109

127-
while(iter.next != null){
128-
if(iter.next!= null && iter.next.val == val){
110+
while (iter.next != null) {
111+
if (iter.next != null && iter.next.val == val) {
129112
temp = iter.next;
130-
iter.next=temp.next;
131-
temp=null;
132-
isHere =true;
113+
iter.next = temp.next;
114+
temp = null;
115+
isHere = true;
133116
break;
134117
}
135-
iter=iter.next;
118+
iter = iter.next;
136119
}
137-
if(!isHere)
138-
System.out.println("Given item is not element of the list.");
139-
120+
if (!isHere)
121+
System.out.println("Given item is not element of the list.");
140122

141123

142124
}
143125

144126
//reverse the list
145-
public void reverseList(){
146-
LinkedList reverseList =new LinkedList();
147-
Node iter= root;
127+
public void reverseList() {
128+
LinkedList reverseList = new LinkedList();
129+
Node iter = root;
148130

149-
while(iter!=null){
150-
reverseList.insertToHead(iter.val);
151-
iter=iter.next;
152-
}
131+
while (iter != null) {
132+
reverseList.insertToHead(iter.val);
133+
iter = iter.next;
134+
}
153135

154-
root= reverseList.root;
155-
tail = reverseList.tail;
136+
root = reverseList.root;
137+
tail = reverseList.tail;
156138
}
157139

158140
//if the given value exists in the list it returns true else it's false
159-
public boolean isExists(int val){
160-
Node iter =root;
161-
boolean result =false;
141+
public boolean isExists(int val) {
142+
Node iter = root;
143+
boolean result = false;
162144

163-
while(iter!= null){
164-
if(iter.val == val){
145+
while (iter != null) {
146+
if (iter.val == val) {
165147
result = true;
166148
break;
167149
}
168-
iter=iter.next;
150+
iter = iter.next;
169151

170152
}
171153

172154
return result;
173155
}
174156

157+
class Node {
158+
int val;
159+
Node next;
175160

176161

162+
public Node(int val) {
163+
this.val = val;
164+
next = null;
165+
}
177166

178-
167+
}
179168

180169

181170
}

0 commit comments

Comments
 (0)