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

Create bubbleSort.java #11

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
79 changes: 79 additions & 0 deletions Minimum_Spanning_Tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import java.util.*;
import java.lang.*;
import java.io.*;

class Minimum_Spanning_Tree {

private static final int V = 5; // Number of vertices in the graph

int minKey(int key[], Boolean mstSet[]){ // function to find the vertex with minimum key value, from the set of vertices not yet included in MST

int min = Integer.MAX_VALUE, min_index = -1;

for (int v = 0; v < V; v++)
if (mstSet[v] == false && key[v] < min) {
min = key[v];
min_index = v;
}

return min_index;
}

void printMST(int parent[], int graph[][]){ //function to print the constructed MST stored in parent[]

System.out.println("\n\nEdge \t Weight");
for (int i = 1; i < V; i++)
System.out.println("\n"+ parent[i] + " - " + i + "\t " + graph[i][parent[i]]);
}


void primMST(int graph[][]) { //Function to construct and print MST for a graph represented using adjacency matrix representation

int parent[] = new int[V]; // to store constructed MST

int key[] = new int[V];

Boolean mstSet[] = new Boolean[V]; // To represent set of vertices included in MST

for (int i = 0; i < V; i++) {
key[i] = Integer.MAX_VALUE;
mstSet[i] = false;
}


key[0] = 0; // this vertex is picked as first vertex

parent[0] = -1; // First node is always root of MST


for (int count = 0; count < V - 1; count++) {

int u = minKey(key, mstSet);

mstSet[u] = true; // Add picked vertex to the MST

for(int v = 0; v < V; v++)
if (graph[u][v] != 0 && mstSet[v] == false && graph[u][v] < key[v]) {
parent[v] = u;
key[v] = graph[u][v];
}
}

printMST(parent, graph);
}

public static void main(String[] args)
{

Minimum_Spanning_Tree t = new Minimum_Spanning_Tree();
int graph[][] = new int[][] { { 0, 1, 0, 10, 3, 0 },
{ 1,0,2,3,0,0 },
{ 0, 2,0,4,0,5 },
{ 10,3,4,0,4,1 },
{ 3,0,0,4,0,0},
{ 0,0,5,1,0,0 }};


t.primMST(graph);
}
}
37 changes: 37 additions & 0 deletions bubbleSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import java.util.Arrays;

class bubbleSort {

// perform the bubble sort
static void bubbleSort(int array[]) {
int size = array.length;

// loop to access each array element
for (int pot = 0; pot < size - 1; pot++)

// loop to compare array elements
for (int j = 0; j < size - i - 1; j++)

// compare two adjacent elements
// change > to < to sort in descending order
if (array[j] > array[j + 1]) {

// swapping occurs if elements
// are not in the intended order
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}

public static void main(String args[]) {

int[] data = { 23, 18, 4, 16, 9, 21, 14 };

// call method using class name
Main.bubbleSort(data);

System.out.println("Sorted Array in Ascending Order:");
System.out.println(Arrays.toString(data));
}
}