We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I think I finally found how to do bubble sort for 2D array and looks like better than doing Array.sort which does no show the true bubble sort algorithm.
int arrayStoreVal[][]= new int[4][4];
/* I know there are 16 values */
/* 4x4 16 Dimensions */
for(int x=0; x < 4; x++){
for(int y=0; y < 4; y++){
arrayStoreVal[x][y] = arr[x][y] + arr[x][y+1] + arr[x][y+2] + arr[x+1][y+1] + arr[x+2][y] + arr[x+2][y+1] + arr[x+2][y+2];
}
}
for(int i = 0; i < arrayStoreVal.length; i++){
for(int j = 0; j < arrayStoreVal.length-1; j++){
for(int k = 0; k < arrayStoreVal.length-1; k++){
if(arrayStoreVal[i][k]>arrayStoreVal[i][k+1]){
int temp = arrayStoreVal[i][k];
arrayStoreVal[i][k] = arrayStoreVal[i][k+1];
arrayStoreVal[i][k+1] = temp;
}
}
}
}
System.out.println(arrayStoreVal[arrayStoreVal.length-1][arrayStoreVal.length-1]);
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 11: 2D Arrays
You are viewing a single comment's thread. Return to all comments →
I think I finally found how to do bubble sort for 2D array and looks like better than doing Array.sort which does no show the true bubble sort algorithm.