Skip to content

Commit 2ee08c2

Browse files
author
phyex0
committed
Bubble sort and linear sort added.
1 parent 7abf8c8 commit 2ee08c2

File tree

5 files changed

+50
-9
lines changed

5 files changed

+50
-9
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/com/company/Algorithms/BubbleSort.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.company.Algorithms;
2+
23
/*
3-
* Bubble Sort =
4-
*
5-
*
6-
*
7-
*
4+
* Bubble Sort:
5+
* Best case = O(n)
6+
* Average case = O(n^2)
7+
* Worst case = O(n^2)
88
*
9-
* */
9+
* This algorithm sorts the array min to max. If current item is greater than the next it swaps.
10+
*/
1011

1112
public class BubbleSort {
1213

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.company.Algorithms;
2+
3+
/*
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+
*/
11+
public class LinearSearch {
12+
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;
18+
break;
19+
}
20+
21+
return index;
22+
23+
}
24+
}

src/com/company/DataStructures/LinkedList.java

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

3+
/*
4+
* Linked List:
5+
*
6+
*
7+
*
8+
*
9+
*
10+
*
11+
* */
12+
313
public class LinkedList {
414

515
class Node{

src/com/company/Main.java

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

33
import com.company.Algorithms.BubbleSort;
4+
import com.company.Algorithms.LinearSearch;
45
import com.company.DataStructures.LinkedList;
56

67
public class Main {
78

89
public static void main(String[] args) {
910
// write your code here
1011
int[] arr = new int[]{3,2,1};
11-
BubbleSort.bubbleSort(arr);
12-
for(int i:arr)
13-
System.out.println(i);
12+
13+
System.out.println(LinearSearch.linearSearch(arr,5));
1414

1515

1616

0 commit comments

Comments
 (0)