I used a bubble sort from online but it seemed to be quite a few characters out. Why is this?
Daniel Whyte
Level 17
Bubble sort
Under discussion
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
16 March 2021, 17:06
How do you want us to answer this if there is no code to consider?
0
Daniel Whyte
16 March 2021, 18:07
I thought it had attached my attempt, can't see how to atttach it now.
I've copied it as:
public static void sort(int[] array) {
//write your code here
int[] arr = new int[30];
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]){
//swap elements
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
+1
Guadalupe Gagnon
16 March 2021, 18:15useful
This code is sorting the values of "arr", which is only declared on the 3rd line. Because it is only declared all the elements in the array will be the default value of 0. This code would do nothing with the values in "array", which is the array that needs to be sorted. Get rid of that extra array and change the rest of the code to use "array".
+3