Okay, it's challenge time for arrays.
This is the challenge video for arrays.
I'm gonna type out what the challenge is now.
Okay, so the challenge is to create a program using arrays, and
it should sort a list of integers in descending order.
Now, descending order is highest value to lowest.
In other words, if the array was five elements in length and
had the values of 106, 26, 81, 5 and 15, then your program should
ultimately have the array sorted so that element position 0 would be 106,
position 1 would be 81, position 2 would be 26, 3 15, and position 4 number 5.
In other words, highest to lowest.
Now, in terms of the numbers that you're gonna be sorting, set up the program so
that these numbers are read in from the keyboard.
I showed you in the last video how to do that.
The methods you actually wanna implement are getIntegers, printArray, and
sortIntegers.
Get integers is gonna return the array of the entered integers from the keyboard
that have been typed in, print array will print out the contents of the array,
and the sort integers method should sort the array and
return a new array containing the sorted numbers.
What you'll have to do apart from figuring out how to actually sort the array,
you'll have to figure out how to copy the array elements from the past array and
create a new array that you can actually work on and sort,
and then you gotta return that new sorted array.
It's time now to pause the video, go away, and actually try out this challenge.
When you're stuck or when you're finished and
you're ready to check my version of the solution, come back,
and I'll show you how to do it, so pause the video now and I'll see you shortly.
Okay, so how did you get on.
Did you figure it out?
Let's go through and create our solutions.
The first thing we want to do is create a private field so
that we can actually import some data from the console, the keyboard.
We're gonna create our Scanner class again, our field.
It's System.in again, and I'm going to actually start down here.
Leave that in there, so
we can refer back to it and make sure we haven't forgotten anything.
Create our integer array.
We're gonna call it myIntegers = get integers.
Let's start with, say, five numbers.
We can always change that later.
Pick five numbers that we're going to sort.
What we need to do is, create our getIntegers method.
You saw that in the last video, but we're gonna create that again.
Public static int, we wanna return the array, getIntegers, and
int capacity, that's gonna be the size of our array.
In this case, it's going to be five, and
so define our array, new int capacity,
and we'll type in a message, Enter.
Put our \r there again to go to the new line.
Then we're going to create our for
loop for i = 0, i < array.length,
i++ Array[i] = scanner.nextInt.
Lastly, we wanna return our array.
That's our getIntegers.
Nothing particularly complex there cuz you saw that in the previous video.
Well, next we'll create our print array, and that's gonna be also very simple.
It's going to accept an array and just print out the contents,
so we're gonna type public static void printarray, int array.
Then we're gonna put a four loop.
For int i=0, i<array.length,
i++ and we'll put,
Element contents array [i].
Pretty simple, as you can see there.
Nothing particularly difficult about that at all.
That's the first two methods, and the, the third one, that is the new one.
This is the sortIntegers method that's going to sort the array and
return a new array containing the sorted numbers, but we've got to figure out a way
how to copy the array elements from the past array into a new array, and
sort them and return the new sorted array.
In other words, we need to copy the contents of that array into a new one.
We haven't actually seen how to do that yet, so how did you get on with that?
Did you ever think about how to do that?
I'll just come up and show you my version of the code,
and what we'll do is we'll put public static.
It's gonna be an int because ultimately you're gonna be returning that array,
sortIntegers, but it's also going to be past the array,
and the array is the unsorted numbers from the keyboard,
essentially that have been typed in from the keyboard.
There's a number of ways of actually doing this, so one of the first things we need
to do is, we're gonna put int, create a variable called sortedArray.
What we're gonna do is, we're gonna set it up and put it = new int array.length.
We're creating an array that is exactly the same size as the array that has been
passed to us.
At the moment, that hasn't got any values in it, so it's going to use a for
loop, for(int i = 0; i < array.length; i++).
Then we can just put sortedArray[i] = array[i], and that's it.
That's creating a copy of the contents that are in the array that's passed to us
into our new array that we've created.
Again, the first line creates the array called sortedArray of type integer, and
we're setting it to the same capacity as the array that's been
passed to this method.
Then we're using a for loop to go through and retrieve each element out of array[i],
and we're actually saving the contents of array at element i into our sorted array.
Consequently, at the end of that for statement,
we've actually got an array that contains the same values as has been passed to us.
The next thing we need to do now is, this is where the complex part comes in.
This is sorting the array, so what we need to do is,
we actually need to create a while loop.
Within that, we need to create a for
loop, and I'm gonna tell you why as we go through.
First, I'm gonna create our boolean called flag.
Now set that to true, and
our while loop is gonna continue processing until flag is actually false.
Now, we also need a temporary variable,
which I'll talk to you about shortly as to why we need that.
Let's make a bit more space here for this method.
What we now need to do, again, is we need to go through the sorted array.
We need to figure out a way for this to be automatically updated so
that we end up having the highest values at the start of the array right through,
element by element, to the end of the array's got the smallest amount.
Let’s start off with our while loop.
It’s gonna be while(flag).
Now, first thing we're gonna do in the while(flag) is,
we're gonna set it to false, and the reason is, we're gonna have a for loop.
If the for loop hasn't sorted all the numbers,
we're going to assign that back to true again so
that the actual while method, or while loop, continues.
By default we're setting it to false, which means if we don't reset flag,
the variable to true, we're gonna automatically end the while loop.
Next comes the code we need to check.
We need to actually do a for loop, so
for(int i = 0; i<sortedArray because we did
it with sorted array now, sortedArray.length, i++.
Next we're going to do a test.
We're going to put, if (sortedArray(i) < sortedArray(i+1),
but the other thing I should have done here is put a minus one,
and again, I'm gonna explain that shortly.
We're gonna type temp = sortedArray.
I think it's easier if I type in the code, and
then I'm gonna go through and explain it all.
So, sortedArray(i) = sortedArray(i+1),
and then sortedArray(i+1) = temp,
=temp, and then we're going to assign flag to true.
Okay, the last thing we're gonna do down here at the end of the while loop,
we're going to return our sortedArray.
Okay, that's the code, so let's go through this while loop and
then the embedded for loop.
The first thing is, we have a while flag, so
we're basically gonna only continue this loop until a while flag is set to true.
Initially, when we first created this method, or we first go into that while
loop for the first time, it's set to true, so it's gonna execute at least once.
Then we immediately set that to false, and the reason,
is we've got to test in here, and we're only going to
try to continue the while loop if we actually assigned flagged to true.
What we're trying to do here is, we're gonna go through each element of
the entire array each time we go through this pass.
One pass at a time, and we're only going to exit the loop if we've actually sorted
all the numbers, and there's nothing left to sort.
How does this actually work?
Well, how it works is, first, with our four loop,
we start out always at element zero, and we're always going to finish.
Our ending element is always on less than the total length of the array, and
the reason is, we've got this (i+1) here.
What we're testing is, we're saying, if the sortedArray at element 0 is less than
the sorted array at the next element in number.
In this case, if we started on 0, it would be element 1.
What that means is, the number in the next element position is actually higher than
that first element, so consequently, we need to swap them.
What we then do is we actually store the current value of the sortedArray into
a temporary variable.
It's there just to actually store that number temporarily.
We then actually move, effectively, the element that's in this (i+1) position
into that area, so we're actually swapping them, and
then we actually assign the temporary variable back.
Really, these three lines of code here, what we're doing is,
we're taking the entry that is at element position 1, for argument's sake, and
swapping it with element position 0.
Then what we're doing, because we've made a change,
what that means is, we're not sure that there's not more changes to be made, so
we need to use the while loop to go through and test,
which ultimately will actually go through this entire for loop again.
Looking to see whether any numbers at a certain position have got high numbers
in one element above them, if that makes sense.
Just to make it so this makes a little bit more sense, let's just presume we've got,
say, element 0, and we'll say that it had the value of 50.
Element 1 had the value of 160.
Element 2, a value of 40.
Let's just say we had those three elements.
Basically, we're going through our loop, we're starting on position 0, and
we're saying if sortedArray[i], and i is 0.
We're saying, if sortedArray[i], which is 50,
is less than sortedArray(i+1), which is 160.
In other words, what we're saying there is, is 50 less than 160?
In that case, that is true.
Then what we do is, we're actually swapping the values around, so,
in this case, we're putting temp = sortedArray[i], again, i is 0.
sortedArray[i] = sortedArray(i+1), and sortedArray(i+1) = temp.
What we're basically doing is, we're going on 60, 50, like so, so
we swap those elements around.
We're setting that flag to true so
that we know that's gonna be going through at least one more time, and
then that actually goes through the rest of the elements.
The second case, i will be set to one, so it's gonna look at 50.
It's gonna say, is 50, it that less than 40?
Cuz 40 is gonna be element 2, (i+1).
In that case, that's gonna be false, so this code doesn't get executed, and
that's because we've got them in the right order already.
But, because flag has already been set true the first time,
it's gonna go through and start again.
The next time it goes through, we're assigning flag to false again, and
this time, the second time it goes through, it does this test.
It's going to start again at element 0 and say, element 0 is 160.
Is 160 less than 50?
Well, this case that's not true anymore, so this code isn't going to be executed,
and likewise for the next test, that's also not gonna be executed.
Flag is gonna continue to be false, and
we're gonna exit out of the while loop at that point and then return to sortedArray.
That's how that actually works.
That's what that sort is doing.
It's just going through one element at a time in the array and
figuring out what needs to change, so that's basically it.
That's our method, our code, to actually perform this, get this to work.
What we now need to do is update our main method above to process this.
What we're going to do is actually, we've got one higher.
At the moment, we've only got the numbers, so we're gonna print those out after we've
sorted them cuz we'll be able to see on the screen what we've typed.
We're going to put int sorted =
sortIntegers(myIntegers), like so,
and we're going to put printArray(sorted).
Let's run that and see what it does.
Enter five values.
Let's enter the values, we'll do the values that we had on the screen.
I'll actually end that.
I'll stop the program, and we'll actually run it again.
Okay, so we're going to enter 50, which is what we had initially.
160, 40, and let's say, 23, 200, and you can see what's done there.
It's actually element 0.
The contents is now 200, 160, 50, 40, and 23, so it's actually
gone through this loop there multiple times until it had nothing else to change.
It's actually sorted in the right order, highest values to lowest values.
That's what the sortArray method, the sortIntegers method is actually doing.
One last thing I'm gonna show you.
We're gonna be talking more about this in future videos, but
the method that I showed you here to create, actually this line as well,
to actually copy an array, that's one way of doing it.
There's actually a number of built-in ways to do this sort of thing, to copy methods.
More and more as you become more familiar with Java,
you'll be wanting to use some of the features that are built in to Java because
it just actually saves you a lot of time.
Less code, and you're actually using code that has been heavily optimized,
much faster, and it's just less code to debug.
An alternative method to copy an array, copy the past array to
the sorted array method, would be to do something like this.
I'm gonna comment that bit of code out.
Certainly optional, but it's just for future reference, so
I'm gonna put sorted Array =.
This time I'm gonna use a class called arrays,
which is built into Java.util, so you can see there.
Arrays., and we can type copy of, and
you can see by looking at a lot of these different methods, they're overwritten.
You can booleans, bytes, characters, doubles, floats,
there's lots of them there.
We're gonna use the first one, copy of.
Copy of, and all we need to do is pass in the array and
then how many characters we want the new array to be.
We're just gonna put array.length, so
that one line there affectively takes a copy of the array with the right length
and automatically creates the new array called sortedArray for us.
We're looking a lot more at, as we go through the course as I said,
at different functions that do things like this
that are built into Java that save you a lot of time.
Just to confirm that 50, 160,
200, 43, 770, and you can see it still works.
770, 200, 160, 50, and 43.
That's the solution to the array challenge.
I hope you enjoyed that, and I will see you in the next video.