Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Java Programs #414

Merged
merged 4 commits into from
Oct 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Java_Program/Bubble_sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public class BubbleSortExample {
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){

temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}

}
}

}
public static void main(String[] args) {
int arr[] ={30,45,12,2,89,3,400};

System.out.println("Array Before Bubble Sort");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();

bubbleSort(arr);

System.out.println("Array After Bubble Sort");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}

}
}
26 changes: 26 additions & 0 deletions Java_Program/Frequency of number in array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Frequency {
public static void main(String[] args) {
int [] arr = new int [] {9, 2, 5, 3, 5, 2, 9, 5, 0};

int [] f = new int [arr.length];
int m = -1;
for(int i = 0; i < arr.length; i++){
int count = 1;
for(int j = i+1; j < arr.length; j++){
if(arr[i] == arr[j]){
count++;

fr[j] = m;
}
}
if(f[i] != m)
f[i] = count;
}
System.out.println(" Element | Frequency");

for(int i = 0; i < f.length; i++){
if(f[i] != m)
System.out.println(" " + arr[i] + " | " + fr[i]);
}

}}
25 changes: 25 additions & 0 deletions Java_Program/binary_search.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class BinarySearchExample{
public static void binarySearch(int arr[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[mid] == key ){
System.out.println("Element is found at index: " + mid);
break;
}else{
last = mid - 1;
}
mid = (first + last)/2;
}
if ( first > last ){
System.out.println("Element is not found!");
}
}
public static void main(String args[]){
int arr[] = {1,2,3,4,5};
int key = 5;
int last=arr.length-1;
binarySearch(arr,0,last,key);
}
}
35 changes: 35 additions & 0 deletions Java_peograms/Bubble_sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public class BubbleSortExample {
static void bubbleSort(int[] arr) {
int n = arr.length;
int temp = 0;
for(int i=0; i < n; i++){
for(int j=1; j < (n-i); j++){
if(arr[j-1] > arr[j]){

temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}

}
}

}
public static void main(String[] args) {
int arr[] ={3,60,35,2,45,320,5};

System.out.println("Array Before Bubble Sort");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}
System.out.println();

bubbleSort(arr);//sorting array elements using bubble sort

System.out.println("Array After Bubble Sort");
for(int i=0; i < arr.length; i++){
System.out.print(arr[i] + " ");
}

}
}