Sorting an Array of int using BubbleSort
You need two loops to implement the Bubble Sort .
Sample code :
public static void bubbleSort(int[] numArray) {
int n = numArray.length;
int temp = 0;
for (int i = 0; i < n; i++) {
for (int j = 1; j < (n - i); j++) {
if (numArray[j - 1] > numArray[j]) {
temp = numArray[j - 1];
numArray[j - 1] = numArray[j];
numArray[j] = temp;
}
}
}
}
Sorting an array with bubble sort
Two issues:
- You should only swap values when they are not in the right order
- You need an outer loop to repeat this inner loop, similar to a proper bubble sort algorithm.
Your code can be modified like below so it generates the required output:
$x = array (9,7,5,3,0);
$count = count($x) - 1;
for($times = 0; $times < $count; $times++) {
for($i = 0; $i < $count; $i++ ) {
$temp = $x[$i+1];
if ($temp < $x[$i]) {
$x[$i+1] = $x[$i];
$x[$i] = $temp;
}
echo implode(" ", $x) . "\n";
}
echo "\n";
}
Note that a proper bubble sort algorithm will perform fewer iterations.
Sorting Array of Structure by Mutiple Criteria with Bubble Sort in C
Just check for the condition while sorting in bubble sort
for (int i = 0; i < nV; i++) {
for (int j = 0; j < nV; j++) {
if ((u[i].nFollower > u[j].nFollower) || \
(u[i].nFollower == u[j].nFollower && u[i].Follow > u[j].Follow) || \
(u[i].nFollower == u[j].nFollower && u[i].Follow == u[j].Follow && i < j)) {
temp1 = u[j].nFollow;
temp2 = u[j].nFollower;
temp3 = u[j].node;
u[j].nFollow = u[i].nFollow;
u[i].nFollow = temp1;
u[j].nFollower = u[i].nFollower;
u[i].nFollower = temp2;
u[j].node = u[i].node;
u[i].node = temp3;
}
}
}
here we are iterating n-1 time and each time we iterate we skip the elements which are in proper order. Instead of using temperory variable I am using xor
operator to swap elements.
for (int i = 0; i < nV-1; i++) {
for (int j = 0; j+1 <= nv-1-i; j++) {
if ((u[j].nFollower > u[j+1].nFollower) || \
(u[j].nFollower == u[j+1].nFollower && u[j].Follow > u[j+1].Follow)) {
u[j].nFollow ^= u[j+1].nFollow;
u[j+1].nFollow ^= u[j].nFollow;
u[j].nFollow ^= u[j+1].nFollow;
u[j].nFollower ^= u[j+1].nFollower;
u[j+1].nFollower ^= u[j].nFollower;
u[j].nFollower ^= u[j+1].nFollower;
u[j].node ^= u[j+1].node;
u[j+1].node ^= u[j].node;
u[j].node ^= u[j+1].node;
}
}
}
2D bubble sort java program that have string and integer in array
This should work. I'll go through the few mistakes you had in your code.
public class Qwerty{
static int[] bubbleSort(String arr[][]) {
int n = arr.length;
int temp = 0;
int []myAge = new int[n];
for (int i = 0; i < n; i++)
{
myAge[i] = Integer.parseInt(arr[i][1]);
}
boolean sorted = false;
while (!sorted){
sorted = true;
for(int i = 1; i < n; i++) {
if(myAge[i-1] > myAge[i]) {
temp = myAge[i-1];
myAge[i-1] = myAge[i];
myAge[i] = temp;
sorted = false;
}
}
}
return myAge;
}
public static void main(String[] args) {
Qwerty bs = new Qwerty();
String arr [][] = {{"Ace","10"},
{"Ben","8"},
{"Cid","20"},
{"Dan","5"},
{"Eve","12"}};
int myAge[] = bs.bubbleSort(arr);
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i][0]+ myAge[i]);
}
}
Firstly, when you did Integer.parseInt
, you were only doing it for j, and not j-1, which means that in the first iteration you would be comparing 8 with 0 (which is the default value for an int array element) when you did if(myAge[j-1] > myAge[j])
. I made this more clear by just parsing the array in a separate loop beforehand.
Secondly, in your bubble sort, the way you know that the sorting is finished is when you are able to go through the array without making any swaps. That's what I did with the sorted
boolean.
Thirdly, in your main method, when you just do bs.bubbleSort(arr)
, nothing will happen because the method returns void. So, I changed the method to return an int array and saved it to the myAge variable in the main method.
I hope this helps.
EDIT: OOPS, after reading your comment I see that you also want the names to correspond with each number, my bad. Here's the new code, and I'll explain what I changed.
public class Qwerty{
static String[][] bubbleSort(String arr[][]) {
int n = arr.length;
int tempAge = 0;
String tempName = "";
int []myAge = new int[n];
String myName[] = new String [n];
for (int i = 0; i < n; i++)
{
myAge[i] = Integer.parseInt(arr[i][1]);
myName[i] = arr[i][0];
}
boolean sorted = false;
while (!sorted){
sorted = true;
for(int i = 1; i < n; i++) {
if(myAge[i-1] > myAge[i]) {
tempAge = myAge[i-1];
myAge[i-1] = myAge[i];
myAge[i] = tempAge;
tempName = myName[i-1];
myName[i-1] = myName[i];
myName[i] = tempName;
sorted = false;
}
}
}
for (int i = 0; i < arr.length; i++)
{
arr[i][0] = myName[i];
arr[i][1] = Integer.toString(myAge[i]);
}
return arr;
}
public static void main(String[] args) {
Qwerty bs = new Qwerty();
String arr [][] = {{"Ace","10"},
{"Ben","8"},
{"Cid","20"},
{"Dan","5"},
{"Eve","12"}};
arr = bs.bubbleSort(arr);
for(int i = 0; i < arr.length; i++) {
System.out.println(arr[i][0]+ arr[i][1]);
}
}
I made the bubbleSort method return the sorted String[][] instead of just an int array like before. I created a separate array for the names which is the myName
array. This is so that in the bubble sort, whenever a swap is made between 2 ages, the names are swapped as well, using the tempName
variable. After all swaps are made to the myName
and myAge
arrays, I use a loop to put them back into the String arr[][], and then return it.
Sorry about that, I should have read your question more carefully. Thanks.
Related Topics
How to Match "Any Character" in Regular Expression
Jpa: What Is the Proper Pattern for Iterating Over Large Result Sets
Passing Pointers Between C and Java Through Jni
Java - How to Receive Point Coordinates After Mouse Button Release (Jfreechart)
Java - Removing First Character of a String
Convert String to Int Array in Java
Why Do We Have to Override the Equals() Method in Java
Check If a File Is Locked in Java
Does Java Have Buffer Overflows
Throw Checked Exceptions from Mocks with Mockito
Why Is "Final" Not Allowed in Java 8 Interface Methods
Why Is Java.Util.Observable Not an Abstract Class
Exception in Thread 'Main' Java.Lang.Noclassdeffounderror:
Comparing Time Is Incorrect When Picking 12:00
Jackson: How to Prevent Field Serialization
Exception in Initializer Error in Java When Using Netbeans