COMPSCI4039 Programming
Arrays Part 4
What is this code doing?
Copyright By PowCoder代写 加微信 powcoder
public class WhatAmI {
public static void main(String[] args) {
int[] numbers = {8,5,4,2,9};
myMethod(numbers);
for(int i=0;i
anyChanged = true;
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}while(anyChanged);
public static void myMethod(int[] a) { . . . }
Insertion order
• When we insert object or primitive values into an array the order is retained.
• So something at offset 1 will always be there at offset 1.
• Sometimes we may want to have the contents of an array ordered for some reason.
• As there is no guarantee that when it was created, it was sorted.
• That is what the code previously was doing.
• It was sorting in place, which means it didn’t create a new array which is sorted.
• It sorted the original array itself.
What is this code doing?
• This approach to sorting values in an array is called bubble sort.
• It is inefficient, but is one of the simplest methods of sorting an array.
• It is called bubble sort as we can think of it being the result of multiple sorting methods.
public class WhatAmI {
public static void main(String[] args) {
int[] numbers = {8,5,4,2,9};
myMethod(numbers);
for(int i=0;i
anyChanged = true;
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}while(anyChanged);
public static void myMethod(int[] a) { . . . }
Mental model
• Blue and green represent 2 values that are considered on each ‘bubble sort’
• You can see this process occurs multiple times before we have the array sorted.
• The boolean value anyChanged controls whether we finish sorting or not.
What is this code doing?
• The method that does the sorting is void, why is this?
• Because when something is done in place, we don’t create a new object at all, rather we manipulate an existing object.
• Here we are manipulating the array, by rearranging the values.
public class WhatAmI {
public static void main(String[] args) {
int[] numbers = {8,5,4,2,9};
myMethod(numbers);
for(int i=0;i
anyChanged = true;
int temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}while(anyChanged);
public static void myMethod(int[] a) { . . . }
What if we want to sort 2 arrays?
• Let’s say we are developing a game…
• We want to track usernames and scores.
• So we have an array for names and an array for scores.
• Will the bubble sort method from the previous example work? • Let’s see!
It didn’t work…?
• Because we have multiple arrays, when sorting one we need to also modify the other.
• This is important as the order for each array is not intrinsically shared. • We need to make that connection ourselves.
• So what do we want to do?
• Let’s see!
“Computers don’t make mistakes, programmers make mistakes”
Why temps?
• If we didn’t have temp variables, the second we change the offsets, we lose data.
• Temps allows us to store data in a temporary location before moving it afterwards as below:
Conclusions
• Paired arrays are not always a good idea.
• Having separate, yet related data is often considered poor
programming practice.
• It goes against the fundamental principles of using objects!
• The rule of thumb is if you have multiple arrays of related data…
• Think of a way to perhaps create objects with them instead.
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com