How to sort a 2D array using bubble sort?










0














I need to sort a 2D array in descending order by row using bubble sort based on the last column but I'm having some trouble with it.



This is the data that I need to arrange in descending order but only based on the last column.



6814.00 85.00 86.00 92.00 88.00 87.75
7234.00 76.00 81.00 84.00 78.00 79.75
6465.00 87.00 54.00 68.00 72.00 70.25
7899.00 92.00 90.00 88.00 86.00 89.00
9901.00 45.00 78.00 79.00 80.00 70.50
8234.00 77.00 87.00 84.00 98.00 86.50
7934.00 76.00 91.00 84.00 65.00 79.00
7284.00 56.00 81.00 87.00 98.00 80.50
7654.00 76.00 87.00 84.00 88.00 83.75
3534.00 86.00 81.00 84.00 73.00 81.00


This is what I have so far.



for(int i = 0; i < 10; i++)

for(int j = 0; j < 6; j++)

if(arr1[i][5] < arr1[i+1][5])

int temp = arr1[i][j];
arr1[i][j] = arr1[i+1][j];
arr1[i+1][j] = temp;





But this is what I get, which clearly isn't working.



 6814.00 85.00 86.00 92.00 88.00 87.75
7234.00 76.00 81.00 84.00 78.00 79.75
7899.00 92.00 90.00 88.00 86.00 89.00
9901.00 45.00 78.00 79.00 80.00 70.50
8234.00 77.00 87.00 84.00 98.00 86.50
7934.00 76.00 91.00 84.00 65.00 79.00
7284.00 56.00 81.00 87.00 98.00 80.50
7654.00 76.00 87.00 84.00 88.00 83.75
3534.00 86.00 81.00 84.00 73.00 81.00
6465.00 87.00 54.00 68.00 72.00 70.00


I also noticed that some of the numbers in the last column got rounded up and I'm not sure why. I appreciate any help I can get.










share|improve this question





















  • Do you want to sort the matrix row wise or column wise? For sorting there is an inbuilt function sort(array name, length) by STL in algorithm.h
    – Asheesh Sahu
    Nov 12 at 4:39











  • Row wise please.
    – something
    Nov 12 at 4:40










  • I have to use bubble sort because it’s a project for school.
    – something
    Nov 12 at 4:48










  • Do you have any complexity issues?
    – Asheesh Sahu
    Nov 12 at 4:58










  • I don’t think so?
    – something
    Nov 12 at 5:04















0














I need to sort a 2D array in descending order by row using bubble sort based on the last column but I'm having some trouble with it.



This is the data that I need to arrange in descending order but only based on the last column.



6814.00 85.00 86.00 92.00 88.00 87.75
7234.00 76.00 81.00 84.00 78.00 79.75
6465.00 87.00 54.00 68.00 72.00 70.25
7899.00 92.00 90.00 88.00 86.00 89.00
9901.00 45.00 78.00 79.00 80.00 70.50
8234.00 77.00 87.00 84.00 98.00 86.50
7934.00 76.00 91.00 84.00 65.00 79.00
7284.00 56.00 81.00 87.00 98.00 80.50
7654.00 76.00 87.00 84.00 88.00 83.75
3534.00 86.00 81.00 84.00 73.00 81.00


This is what I have so far.



for(int i = 0; i < 10; i++)

for(int j = 0; j < 6; j++)

if(arr1[i][5] < arr1[i+1][5])

int temp = arr1[i][j];
arr1[i][j] = arr1[i+1][j];
arr1[i+1][j] = temp;





But this is what I get, which clearly isn't working.



 6814.00 85.00 86.00 92.00 88.00 87.75
7234.00 76.00 81.00 84.00 78.00 79.75
7899.00 92.00 90.00 88.00 86.00 89.00
9901.00 45.00 78.00 79.00 80.00 70.50
8234.00 77.00 87.00 84.00 98.00 86.50
7934.00 76.00 91.00 84.00 65.00 79.00
7284.00 56.00 81.00 87.00 98.00 80.50
7654.00 76.00 87.00 84.00 88.00 83.75
3534.00 86.00 81.00 84.00 73.00 81.00
6465.00 87.00 54.00 68.00 72.00 70.00


I also noticed that some of the numbers in the last column got rounded up and I'm not sure why. I appreciate any help I can get.










share|improve this question





















  • Do you want to sort the matrix row wise or column wise? For sorting there is an inbuilt function sort(array name, length) by STL in algorithm.h
    – Asheesh Sahu
    Nov 12 at 4:39











  • Row wise please.
    – something
    Nov 12 at 4:40










  • I have to use bubble sort because it’s a project for school.
    – something
    Nov 12 at 4:48










  • Do you have any complexity issues?
    – Asheesh Sahu
    Nov 12 at 4:58










  • I don’t think so?
    – something
    Nov 12 at 5:04













0












0








0







I need to sort a 2D array in descending order by row using bubble sort based on the last column but I'm having some trouble with it.



This is the data that I need to arrange in descending order but only based on the last column.



6814.00 85.00 86.00 92.00 88.00 87.75
7234.00 76.00 81.00 84.00 78.00 79.75
6465.00 87.00 54.00 68.00 72.00 70.25
7899.00 92.00 90.00 88.00 86.00 89.00
9901.00 45.00 78.00 79.00 80.00 70.50
8234.00 77.00 87.00 84.00 98.00 86.50
7934.00 76.00 91.00 84.00 65.00 79.00
7284.00 56.00 81.00 87.00 98.00 80.50
7654.00 76.00 87.00 84.00 88.00 83.75
3534.00 86.00 81.00 84.00 73.00 81.00


This is what I have so far.



for(int i = 0; i < 10; i++)

for(int j = 0; j < 6; j++)

if(arr1[i][5] < arr1[i+1][5])

int temp = arr1[i][j];
arr1[i][j] = arr1[i+1][j];
arr1[i+1][j] = temp;





But this is what I get, which clearly isn't working.



 6814.00 85.00 86.00 92.00 88.00 87.75
7234.00 76.00 81.00 84.00 78.00 79.75
7899.00 92.00 90.00 88.00 86.00 89.00
9901.00 45.00 78.00 79.00 80.00 70.50
8234.00 77.00 87.00 84.00 98.00 86.50
7934.00 76.00 91.00 84.00 65.00 79.00
7284.00 56.00 81.00 87.00 98.00 80.50
7654.00 76.00 87.00 84.00 88.00 83.75
3534.00 86.00 81.00 84.00 73.00 81.00
6465.00 87.00 54.00 68.00 72.00 70.00


I also noticed that some of the numbers in the last column got rounded up and I'm not sure why. I appreciate any help I can get.










share|improve this question













I need to sort a 2D array in descending order by row using bubble sort based on the last column but I'm having some trouble with it.



This is the data that I need to arrange in descending order but only based on the last column.



6814.00 85.00 86.00 92.00 88.00 87.75
7234.00 76.00 81.00 84.00 78.00 79.75
6465.00 87.00 54.00 68.00 72.00 70.25
7899.00 92.00 90.00 88.00 86.00 89.00
9901.00 45.00 78.00 79.00 80.00 70.50
8234.00 77.00 87.00 84.00 98.00 86.50
7934.00 76.00 91.00 84.00 65.00 79.00
7284.00 56.00 81.00 87.00 98.00 80.50
7654.00 76.00 87.00 84.00 88.00 83.75
3534.00 86.00 81.00 84.00 73.00 81.00


This is what I have so far.



for(int i = 0; i < 10; i++)

for(int j = 0; j < 6; j++)

if(arr1[i][5] < arr1[i+1][5])

int temp = arr1[i][j];
arr1[i][j] = arr1[i+1][j];
arr1[i+1][j] = temp;





But this is what I get, which clearly isn't working.



 6814.00 85.00 86.00 92.00 88.00 87.75
7234.00 76.00 81.00 84.00 78.00 79.75
7899.00 92.00 90.00 88.00 86.00 89.00
9901.00 45.00 78.00 79.00 80.00 70.50
8234.00 77.00 87.00 84.00 98.00 86.50
7934.00 76.00 91.00 84.00 65.00 79.00
7284.00 56.00 81.00 87.00 98.00 80.50
7654.00 76.00 87.00 84.00 88.00 83.75
3534.00 86.00 81.00 84.00 73.00 81.00
6465.00 87.00 54.00 68.00 72.00 70.00


I also noticed that some of the numbers in the last column got rounded up and I'm not sure why. I appreciate any help I can get.







c arrays sorting multidimensional-array 2d






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 at 4:11









something

43




43











  • Do you want to sort the matrix row wise or column wise? For sorting there is an inbuilt function sort(array name, length) by STL in algorithm.h
    – Asheesh Sahu
    Nov 12 at 4:39











  • Row wise please.
    – something
    Nov 12 at 4:40










  • I have to use bubble sort because it’s a project for school.
    – something
    Nov 12 at 4:48










  • Do you have any complexity issues?
    – Asheesh Sahu
    Nov 12 at 4:58










  • I don’t think so?
    – something
    Nov 12 at 5:04
















  • Do you want to sort the matrix row wise or column wise? For sorting there is an inbuilt function sort(array name, length) by STL in algorithm.h
    – Asheesh Sahu
    Nov 12 at 4:39











  • Row wise please.
    – something
    Nov 12 at 4:40










  • I have to use bubble sort because it’s a project for school.
    – something
    Nov 12 at 4:48










  • Do you have any complexity issues?
    – Asheesh Sahu
    Nov 12 at 4:58










  • I don’t think so?
    – something
    Nov 12 at 5:04















Do you want to sort the matrix row wise or column wise? For sorting there is an inbuilt function sort(array name, length) by STL in algorithm.h
– Asheesh Sahu
Nov 12 at 4:39





Do you want to sort the matrix row wise or column wise? For sorting there is an inbuilt function sort(array name, length) by STL in algorithm.h
– Asheesh Sahu
Nov 12 at 4:39













Row wise please.
– something
Nov 12 at 4:40




Row wise please.
– something
Nov 12 at 4:40












I have to use bubble sort because it’s a project for school.
– something
Nov 12 at 4:48




I have to use bubble sort because it’s a project for school.
– something
Nov 12 at 4:48












Do you have any complexity issues?
– Asheesh Sahu
Nov 12 at 4:58




Do you have any complexity issues?
– Asheesh Sahu
Nov 12 at 4:58












I don’t think so?
– something
Nov 12 at 5:04




I don’t think so?
– something
Nov 12 at 5:04












3 Answers
3






active

oldest

votes


















0














The most simple way is you can take a whole row at a time and then sort it.



for(int i=0;i<10;i++)

int array[6];
for(int j=0;j<6;j++)

array[j] = arr1[i][j];

for(i=0; i<(n-1); i++)

for(j=0; j<(n-i-1); j++)

if(array[j]>array[j+1])

temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;



for(int j=0;j<6;j++)

arr1[i][j] = array[j];







share|improve this answer




















  • What would n be in this case? Do I just initialize it like int n; or do I have to set it to something?
    – something
    Nov 12 at 5:38










  • sorry for my mistake, n should be the length of array i.e., 6
    – Asheesh Sahu
    Nov 12 at 18:43


















0














If you only care about the order of the last column, just check the last column, then move the entire row accordingly:



void BubbleSort2D(double arr1)
for(int m=0; m<10; m++)
for(int i = 0; i < 9; i++)

if(arr1[i][5] < arr1[i+1][5])

for(int j = 0; j < 6; j++)

double temp = arr1[i][j];
arr1[i][j] = arr1[i+1][j];
arr1[i+1][j] = temp;






public static void main(String args)
double arr2D = 6814.00, 85.00, 86.00, 92.00, 88.00, 87.75,
7234.00, 76.00, 81.00, 84.00, 78.00, 79.75,
6465.00 , 87.00, 54.00, 68.00, 72.00, 70.25,
7899.00, 92.00, 90.00, 88.00, 86.00, 89.00,
9901.00, 45.00, 78.00, 79.00, 80.00, 70.50,
8234.00, 77.00, 87.00, 84.00, 98.00, 86.50,
7934.00, 76.00, 91.00, 84.00, 65.00, 79.00,
7284.00, 56.00, 81.00, 87.00, 98.00, 80.50,
7654.00, 76.00, 87.00, 84.00, 88.00, 83.75,
3534.00, 86.00, 81.00, 84.00, 73.00, 81.00 ;
driver.BubbleSort2D(arr2D);
for(int m=0; m<9; m++)
for(int i = 0; i < 6; i++)

System.out.print(arr2D[m][i] + " ");

System.out.println();




Output:



7899.0 92.0 90.0 88.0 86.0 89.0

6814.0 85.0 86.0 92.0 88.0 87.75

8234.0 77.0 87.0 84.0 98.0 86.5

7654.0 76.0 87.0 84.0 88.0 83.75

3534.0 86.0 81.0 84.0 73.0 81.0

7284.0 56.0 81.0 87.0 98.0 80.5

7234.0 76.0 81.0 84.0 78.0 79.75

7934.0 76.0 91.0 84.0 65.0 79.0

9901.0 45.0 78.0 79.0 80.0 70.5






share|improve this answer






















  • So would I add another for loop or something?
    – something
    Nov 12 at 5:05










  • Sure. In the worst case it would take 10 iterations, so just add another 10-loop around the whole thing. I'll edit the snippet.
    – AardvarkBlue
    Nov 12 at 5:54










  • As an aside you should replace all the references to numbers like 9 or 6 with length checks to that array.
    – AardvarkBlue
    Nov 12 at 5:57










  • You're awesome! Thank you, this worked!
    – something
    Nov 12 at 6:36


















0














Check this.



for(int i = 1; i <=9; i++)

for(int j = 9; i <= j; j--) //edit.

if(arr1[j][5] > arr1[j-1][5])

for(int k = 0; k <= 5; k++)

int temp = arr1[j][k];
arr1[j][k] = arr1[j-1][k];
arr1[j-1][k] = temp;









share|improve this answer






















  • I tried this but it didn’t work. It gave me a exit-1 thing in return.
    – something
    Nov 12 at 5:42










  • I typed 1 instead of j. Now it must work.
    – manoliar
    Nov 12 at 7:03










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255872%2fhow-to-sort-a-2d-array-using-bubble-sort%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














The most simple way is you can take a whole row at a time and then sort it.



for(int i=0;i<10;i++)

int array[6];
for(int j=0;j<6;j++)

array[j] = arr1[i][j];

for(i=0; i<(n-1); i++)

for(j=0; j<(n-i-1); j++)

if(array[j]>array[j+1])

temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;



for(int j=0;j<6;j++)

arr1[i][j] = array[j];







share|improve this answer




















  • What would n be in this case? Do I just initialize it like int n; or do I have to set it to something?
    – something
    Nov 12 at 5:38










  • sorry for my mistake, n should be the length of array i.e., 6
    – Asheesh Sahu
    Nov 12 at 18:43















0














The most simple way is you can take a whole row at a time and then sort it.



for(int i=0;i<10;i++)

int array[6];
for(int j=0;j<6;j++)

array[j] = arr1[i][j];

for(i=0; i<(n-1); i++)

for(j=0; j<(n-i-1); j++)

if(array[j]>array[j+1])

temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;



for(int j=0;j<6;j++)

arr1[i][j] = array[j];







share|improve this answer




















  • What would n be in this case? Do I just initialize it like int n; or do I have to set it to something?
    – something
    Nov 12 at 5:38










  • sorry for my mistake, n should be the length of array i.e., 6
    – Asheesh Sahu
    Nov 12 at 18:43













0












0








0






The most simple way is you can take a whole row at a time and then sort it.



for(int i=0;i<10;i++)

int array[6];
for(int j=0;j<6;j++)

array[j] = arr1[i][j];

for(i=0; i<(n-1); i++)

for(j=0; j<(n-i-1); j++)

if(array[j]>array[j+1])

temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;



for(int j=0;j<6;j++)

arr1[i][j] = array[j];







share|improve this answer












The most simple way is you can take a whole row at a time and then sort it.



for(int i=0;i<10;i++)

int array[6];
for(int j=0;j<6;j++)

array[j] = arr1[i][j];

for(i=0; i<(n-1); i++)

for(j=0; j<(n-i-1); j++)

if(array[j]>array[j+1])

temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;



for(int j=0;j<6;j++)

arr1[i][j] = array[j];








share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 at 5:13









Asheesh Sahu

1127




1127











  • What would n be in this case? Do I just initialize it like int n; or do I have to set it to something?
    – something
    Nov 12 at 5:38










  • sorry for my mistake, n should be the length of array i.e., 6
    – Asheesh Sahu
    Nov 12 at 18:43
















  • What would n be in this case? Do I just initialize it like int n; or do I have to set it to something?
    – something
    Nov 12 at 5:38










  • sorry for my mistake, n should be the length of array i.e., 6
    – Asheesh Sahu
    Nov 12 at 18:43















What would n be in this case? Do I just initialize it like int n; or do I have to set it to something?
– something
Nov 12 at 5:38




What would n be in this case? Do I just initialize it like int n; or do I have to set it to something?
– something
Nov 12 at 5:38












sorry for my mistake, n should be the length of array i.e., 6
– Asheesh Sahu
Nov 12 at 18:43




sorry for my mistake, n should be the length of array i.e., 6
– Asheesh Sahu
Nov 12 at 18:43













0














If you only care about the order of the last column, just check the last column, then move the entire row accordingly:



void BubbleSort2D(double arr1)
for(int m=0; m<10; m++)
for(int i = 0; i < 9; i++)

if(arr1[i][5] < arr1[i+1][5])

for(int j = 0; j < 6; j++)

double temp = arr1[i][j];
arr1[i][j] = arr1[i+1][j];
arr1[i+1][j] = temp;






public static void main(String args)
double arr2D = 6814.00, 85.00, 86.00, 92.00, 88.00, 87.75,
7234.00, 76.00, 81.00, 84.00, 78.00, 79.75,
6465.00 , 87.00, 54.00, 68.00, 72.00, 70.25,
7899.00, 92.00, 90.00, 88.00, 86.00, 89.00,
9901.00, 45.00, 78.00, 79.00, 80.00, 70.50,
8234.00, 77.00, 87.00, 84.00, 98.00, 86.50,
7934.00, 76.00, 91.00, 84.00, 65.00, 79.00,
7284.00, 56.00, 81.00, 87.00, 98.00, 80.50,
7654.00, 76.00, 87.00, 84.00, 88.00, 83.75,
3534.00, 86.00, 81.00, 84.00, 73.00, 81.00 ;
driver.BubbleSort2D(arr2D);
for(int m=0; m<9; m++)
for(int i = 0; i < 6; i++)

System.out.print(arr2D[m][i] + " ");

System.out.println();




Output:



7899.0 92.0 90.0 88.0 86.0 89.0

6814.0 85.0 86.0 92.0 88.0 87.75

8234.0 77.0 87.0 84.0 98.0 86.5

7654.0 76.0 87.0 84.0 88.0 83.75

3534.0 86.0 81.0 84.0 73.0 81.0

7284.0 56.0 81.0 87.0 98.0 80.5

7234.0 76.0 81.0 84.0 78.0 79.75

7934.0 76.0 91.0 84.0 65.0 79.0

9901.0 45.0 78.0 79.0 80.0 70.5






share|improve this answer






















  • So would I add another for loop or something?
    – something
    Nov 12 at 5:05










  • Sure. In the worst case it would take 10 iterations, so just add another 10-loop around the whole thing. I'll edit the snippet.
    – AardvarkBlue
    Nov 12 at 5:54










  • As an aside you should replace all the references to numbers like 9 or 6 with length checks to that array.
    – AardvarkBlue
    Nov 12 at 5:57










  • You're awesome! Thank you, this worked!
    – something
    Nov 12 at 6:36















0














If you only care about the order of the last column, just check the last column, then move the entire row accordingly:



void BubbleSort2D(double arr1)
for(int m=0; m<10; m++)
for(int i = 0; i < 9; i++)

if(arr1[i][5] < arr1[i+1][5])

for(int j = 0; j < 6; j++)

double temp = arr1[i][j];
arr1[i][j] = arr1[i+1][j];
arr1[i+1][j] = temp;






public static void main(String args)
double arr2D = 6814.00, 85.00, 86.00, 92.00, 88.00, 87.75,
7234.00, 76.00, 81.00, 84.00, 78.00, 79.75,
6465.00 , 87.00, 54.00, 68.00, 72.00, 70.25,
7899.00, 92.00, 90.00, 88.00, 86.00, 89.00,
9901.00, 45.00, 78.00, 79.00, 80.00, 70.50,
8234.00, 77.00, 87.00, 84.00, 98.00, 86.50,
7934.00, 76.00, 91.00, 84.00, 65.00, 79.00,
7284.00, 56.00, 81.00, 87.00, 98.00, 80.50,
7654.00, 76.00, 87.00, 84.00, 88.00, 83.75,
3534.00, 86.00, 81.00, 84.00, 73.00, 81.00 ;
driver.BubbleSort2D(arr2D);
for(int m=0; m<9; m++)
for(int i = 0; i < 6; i++)

System.out.print(arr2D[m][i] + " ");

System.out.println();




Output:



7899.0 92.0 90.0 88.0 86.0 89.0

6814.0 85.0 86.0 92.0 88.0 87.75

8234.0 77.0 87.0 84.0 98.0 86.5

7654.0 76.0 87.0 84.0 88.0 83.75

3534.0 86.0 81.0 84.0 73.0 81.0

7284.0 56.0 81.0 87.0 98.0 80.5

7234.0 76.0 81.0 84.0 78.0 79.75

7934.0 76.0 91.0 84.0 65.0 79.0

9901.0 45.0 78.0 79.0 80.0 70.5






share|improve this answer






















  • So would I add another for loop or something?
    – something
    Nov 12 at 5:05










  • Sure. In the worst case it would take 10 iterations, so just add another 10-loop around the whole thing. I'll edit the snippet.
    – AardvarkBlue
    Nov 12 at 5:54










  • As an aside you should replace all the references to numbers like 9 or 6 with length checks to that array.
    – AardvarkBlue
    Nov 12 at 5:57










  • You're awesome! Thank you, this worked!
    – something
    Nov 12 at 6:36













0












0








0






If you only care about the order of the last column, just check the last column, then move the entire row accordingly:



void BubbleSort2D(double arr1)
for(int m=0; m<10; m++)
for(int i = 0; i < 9; i++)

if(arr1[i][5] < arr1[i+1][5])

for(int j = 0; j < 6; j++)

double temp = arr1[i][j];
arr1[i][j] = arr1[i+1][j];
arr1[i+1][j] = temp;






public static void main(String args)
double arr2D = 6814.00, 85.00, 86.00, 92.00, 88.00, 87.75,
7234.00, 76.00, 81.00, 84.00, 78.00, 79.75,
6465.00 , 87.00, 54.00, 68.00, 72.00, 70.25,
7899.00, 92.00, 90.00, 88.00, 86.00, 89.00,
9901.00, 45.00, 78.00, 79.00, 80.00, 70.50,
8234.00, 77.00, 87.00, 84.00, 98.00, 86.50,
7934.00, 76.00, 91.00, 84.00, 65.00, 79.00,
7284.00, 56.00, 81.00, 87.00, 98.00, 80.50,
7654.00, 76.00, 87.00, 84.00, 88.00, 83.75,
3534.00, 86.00, 81.00, 84.00, 73.00, 81.00 ;
driver.BubbleSort2D(arr2D);
for(int m=0; m<9; m++)
for(int i = 0; i < 6; i++)

System.out.print(arr2D[m][i] + " ");

System.out.println();




Output:



7899.0 92.0 90.0 88.0 86.0 89.0

6814.0 85.0 86.0 92.0 88.0 87.75

8234.0 77.0 87.0 84.0 98.0 86.5

7654.0 76.0 87.0 84.0 88.0 83.75

3534.0 86.0 81.0 84.0 73.0 81.0

7284.0 56.0 81.0 87.0 98.0 80.5

7234.0 76.0 81.0 84.0 78.0 79.75

7934.0 76.0 91.0 84.0 65.0 79.0

9901.0 45.0 78.0 79.0 80.0 70.5






share|improve this answer














If you only care about the order of the last column, just check the last column, then move the entire row accordingly:



void BubbleSort2D(double arr1)
for(int m=0; m<10; m++)
for(int i = 0; i < 9; i++)

if(arr1[i][5] < arr1[i+1][5])

for(int j = 0; j < 6; j++)

double temp = arr1[i][j];
arr1[i][j] = arr1[i+1][j];
arr1[i+1][j] = temp;






public static void main(String args)
double arr2D = 6814.00, 85.00, 86.00, 92.00, 88.00, 87.75,
7234.00, 76.00, 81.00, 84.00, 78.00, 79.75,
6465.00 , 87.00, 54.00, 68.00, 72.00, 70.25,
7899.00, 92.00, 90.00, 88.00, 86.00, 89.00,
9901.00, 45.00, 78.00, 79.00, 80.00, 70.50,
8234.00, 77.00, 87.00, 84.00, 98.00, 86.50,
7934.00, 76.00, 91.00, 84.00, 65.00, 79.00,
7284.00, 56.00, 81.00, 87.00, 98.00, 80.50,
7654.00, 76.00, 87.00, 84.00, 88.00, 83.75,
3534.00, 86.00, 81.00, 84.00, 73.00, 81.00 ;
driver.BubbleSort2D(arr2D);
for(int m=0; m<9; m++)
for(int i = 0; i < 6; i++)

System.out.print(arr2D[m][i] + " ");

System.out.println();




Output:



7899.0 92.0 90.0 88.0 86.0 89.0

6814.0 85.0 86.0 92.0 88.0 87.75

8234.0 77.0 87.0 84.0 98.0 86.5

7654.0 76.0 87.0 84.0 88.0 83.75

3534.0 86.0 81.0 84.0 73.0 81.0

7284.0 56.0 81.0 87.0 98.0 80.5

7234.0 76.0 81.0 84.0 78.0 79.75

7934.0 76.0 91.0 84.0 65.0 79.0

9901.0 45.0 78.0 79.0 80.0 70.5







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 5:56

























answered Nov 12 at 5:02









AardvarkBlue

264




264











  • So would I add another for loop or something?
    – something
    Nov 12 at 5:05










  • Sure. In the worst case it would take 10 iterations, so just add another 10-loop around the whole thing. I'll edit the snippet.
    – AardvarkBlue
    Nov 12 at 5:54










  • As an aside you should replace all the references to numbers like 9 or 6 with length checks to that array.
    – AardvarkBlue
    Nov 12 at 5:57










  • You're awesome! Thank you, this worked!
    – something
    Nov 12 at 6:36
















  • So would I add another for loop or something?
    – something
    Nov 12 at 5:05










  • Sure. In the worst case it would take 10 iterations, so just add another 10-loop around the whole thing. I'll edit the snippet.
    – AardvarkBlue
    Nov 12 at 5:54










  • As an aside you should replace all the references to numbers like 9 or 6 with length checks to that array.
    – AardvarkBlue
    Nov 12 at 5:57










  • You're awesome! Thank you, this worked!
    – something
    Nov 12 at 6:36















So would I add another for loop or something?
– something
Nov 12 at 5:05




So would I add another for loop or something?
– something
Nov 12 at 5:05












Sure. In the worst case it would take 10 iterations, so just add another 10-loop around the whole thing. I'll edit the snippet.
– AardvarkBlue
Nov 12 at 5:54




Sure. In the worst case it would take 10 iterations, so just add another 10-loop around the whole thing. I'll edit the snippet.
– AardvarkBlue
Nov 12 at 5:54












As an aside you should replace all the references to numbers like 9 or 6 with length checks to that array.
– AardvarkBlue
Nov 12 at 5:57




As an aside you should replace all the references to numbers like 9 or 6 with length checks to that array.
– AardvarkBlue
Nov 12 at 5:57












You're awesome! Thank you, this worked!
– something
Nov 12 at 6:36




You're awesome! Thank you, this worked!
– something
Nov 12 at 6:36











0














Check this.



for(int i = 1; i <=9; i++)

for(int j = 9; i <= j; j--) //edit.

if(arr1[j][5] > arr1[j-1][5])

for(int k = 0; k <= 5; k++)

int temp = arr1[j][k];
arr1[j][k] = arr1[j-1][k];
arr1[j-1][k] = temp;









share|improve this answer






















  • I tried this but it didn’t work. It gave me a exit-1 thing in return.
    – something
    Nov 12 at 5:42










  • I typed 1 instead of j. Now it must work.
    – manoliar
    Nov 12 at 7:03















0














Check this.



for(int i = 1; i <=9; i++)

for(int j = 9; i <= j; j--) //edit.

if(arr1[j][5] > arr1[j-1][5])

for(int k = 0; k <= 5; k++)

int temp = arr1[j][k];
arr1[j][k] = arr1[j-1][k];
arr1[j-1][k] = temp;









share|improve this answer






















  • I tried this but it didn’t work. It gave me a exit-1 thing in return.
    – something
    Nov 12 at 5:42










  • I typed 1 instead of j. Now it must work.
    – manoliar
    Nov 12 at 7:03













0












0








0






Check this.



for(int i = 1; i <=9; i++)

for(int j = 9; i <= j; j--) //edit.

if(arr1[j][5] > arr1[j-1][5])

for(int k = 0; k <= 5; k++)

int temp = arr1[j][k];
arr1[j][k] = arr1[j-1][k];
arr1[j-1][k] = temp;









share|improve this answer














Check this.



for(int i = 1; i <=9; i++)

for(int j = 9; i <= j; j--) //edit.

if(arr1[j][5] > arr1[j-1][5])

for(int k = 0; k <= 5; k++)

int temp = arr1[j][k];
arr1[j][k] = arr1[j-1][k];
arr1[j-1][k] = temp;










share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 7:02

























answered Nov 12 at 5:15









manoliar

12216




12216











  • I tried this but it didn’t work. It gave me a exit-1 thing in return.
    – something
    Nov 12 at 5:42










  • I typed 1 instead of j. Now it must work.
    – manoliar
    Nov 12 at 7:03
















  • I tried this but it didn’t work. It gave me a exit-1 thing in return.
    – something
    Nov 12 at 5:42










  • I typed 1 instead of j. Now it must work.
    – manoliar
    Nov 12 at 7:03















I tried this but it didn’t work. It gave me a exit-1 thing in return.
– something
Nov 12 at 5:42




I tried this but it didn’t work. It gave me a exit-1 thing in return.
– something
Nov 12 at 5:42












I typed 1 instead of j. Now it must work.
– manoliar
Nov 12 at 7:03




I typed 1 instead of j. Now it must work.
– manoliar
Nov 12 at 7:03

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53255872%2fhow-to-sort-a-2d-array-using-bubble-sort%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto