Sorting Arrays

Learn the Basic Java Concepts Loops, arrays and methods
14 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€67.34
List Price:  €96.21
You save:  €28.86
£55.94
List Price:  £79.93
You save:  £23.98
CA$100.65
List Price:  CA$143.79
You save:  CA$43.14
A$112.33
List Price:  A$160.48
You save:  A$48.15
S$95.07
List Price:  S$135.83
You save:  S$40.75
HK$543.93
List Price:  HK$777.07
You save:  HK$233.14
CHF 62.61
List Price:  CHF 89.45
You save:  CHF 26.84
NOK kr799.33
List Price:  NOK kr1,141.95
You save:  NOK kr342.62
DKK kr502.32
List Price:  DKK kr717.64
You save:  DKK kr215.31
NZ$124.25
List Price:  NZ$177.52
You save:  NZ$53.26
د.إ257.07
List Price:  د.إ367.25
You save:  د.إ110.18
৳8,395.96
List Price:  ৳11,994.74
You save:  ৳3,598.78
₹5,947.38
List Price:  ₹8,496.63
You save:  ₹2,549.24
RM315.51
List Price:  RM450.75
You save:  RM135.24
₦108,935.23
List Price:  ₦155,628.43
You save:  ₦46,693.20
₨19,553.63
List Price:  ₨27,934.96
You save:  ₨8,381.32
฿2,411.01
List Price:  ฿3,444.45
You save:  ฿1,033.44
₺2,462.87
List Price:  ₺3,518.54
You save:  ₺1,055.66
B$432.25
List Price:  B$617.53
You save:  B$185.28
R1,286.40
List Price:  R1,837.80
You save:  R551.39
Лв131.81
List Price:  Лв188.31
You save:  Лв56.50
₩101,406.23
List Price:  ₩144,872.25
You save:  ₩43,466.02
₪255.41
List Price:  ₪364.89
You save:  ₪109.47
₱4,117.93
List Price:  ₱5,883.01
You save:  ₱1,765.08
¥10,970.49
List Price:  ¥15,672.80
You save:  ¥4,702.31
MX$1,420.18
List Price:  MX$2,028.91
You save:  MX$608.73
QR256.43
List Price:  QR366.34
You save:  QR109.91
P967.77
List Price:  P1,382.59
You save:  P414.82
KSh9,046.20
List Price:  KSh12,923.70
You save:  KSh3,877.50
E£3,563.73
List Price:  E£5,091.27
You save:  E£1,527.53
ብር8,934.81
List Price:  ብር12,764.56
You save:  ብር3,829.75
Kz64,250.82
List Price:  Kz91,790.82
You save:  Kz27,540
CLP$69,405.58
List Price:  CLP$99,155.08
You save:  CLP$29,749.50
CN¥510.85
List Price:  CN¥729.81
You save:  CN¥218.96
RD$4,272.98
List Price:  RD$6,104.52
You save:  RD$1,831.54
DA9,417.81
List Price:  DA13,454.60
You save:  DA4,036.78
FJ$162.47
List Price:  FJ$232.11
You save:  FJ$69.64
Q541.22
List Price:  Q773.21
You save:  Q231.98
GY$14,699.69
List Price:  GY$21,000.46
You save:  GY$6,300.76
ISK kr9,732.10
List Price:  ISK kr13,903.60
You save:  ISK kr4,171.50
DH705.15
List Price:  DH1,007.40
You save:  DH302.25
L1,289.19
List Price:  L1,841.78
You save:  L552.59
ден4,145.57
List Price:  ден5,922.50
You save:  ден1,776.92
MOP$562.37
List Price:  MOP$803.42
You save:  MOP$241.05
N$1,284.24
List Price:  N$1,834.70
You save:  N$550.46
C$2,585.91
List Price:  C$3,694.32
You save:  C$1,108.40
रु9,565.49
List Price:  रु13,665.58
You save:  रु4,100.08
S/262.28
List Price:  S/374.71
You save:  S/112.42
K284.79
List Price:  K406.86
You save:  K122.07
SAR262.99
List Price:  SAR375.72
You save:  SAR112.72
ZK1,944.48
List Price:  ZK2,777.96
You save:  ZK833.47
L335.15
List Price:  L478.81
You save:  L143.65
Kč1,692.70
List Price:  Kč2,418.25
You save:  Kč725.55
Ft27,859.13
List Price:  Ft39,800.47
You save:  Ft11,941.33
SEK kr772.53
List Price:  SEK kr1,103.66
You save:  SEK kr331.13
ARS$71,530.46
List Price:  ARS$102,190.76
You save:  ARS$30,660.29
Bs485.50
List Price:  Bs693.61
You save:  Bs208.10
COP$306,446.24
List Price:  COP$437,799.12
You save:  COP$131,352.87
₡35,334.71
List Price:  ₡50,480.33
You save:  ₡15,145.61
L1,783.55
List Price:  L2,548.03
You save:  L764.48
₲548,864.71
List Price:  ₲784,126.06
You save:  ₲235,261.34
$U3,122.15
List Price:  $U4,460.41
You save:  $U1,338.25
zł286.96
List Price:  zł409.96
You save:  zł123
Already have an account? Log In

Transcript

Hello there, and welcome back to this Java development course. So last time we went over arrays, and you had some homework to do, your job was to take an array of integers, and then just print the average of that array. So here essentially what should have done. So here I have a double sum, which is zero double average, which is zero, then I iterate over the array, and add to sum the value that I'm iterating over. Then finally I just assign sum divided by a dot length to average and I just print average. And essentially, we're done.

So that let's test it out. There we go. So we can see we get 101.0. It's kind of difficult to test. Let's go ahead and do eight comma eight, just to make sure it's working. And we get 8.0.

There we go. We can do nine as well. Yeah, nine as well and we get 8.5. There we go. So we use doubles here, because if we add integers, then we can add We will not be able to have this point five here. So for because so because of that we need to use doubles.

So yeah, pretty simple. Okay, so that's actually how we should have done if you got that it's very good if not to understand what this code does, and it'll be fine. So anyway, let's get into this lesson. Alright, so today we're going to be going over sorting arrays. So we're going to go ahead and now create a new class called sorting arrays. All right, there we go.

Main is always finish. Alright, and now we're going to do int, a equals 54321. Let's just make it like that for now. All right, so our job is to sort this array. Now, sorting arrays is actually a pretty big thing. So especially if you're working at one of those very large companies that have a huge amount of data like Google, you know, Amazon, Facebook, large companies.

Sorting is actually a pretty big topic, since they work with so much data, that if you can increase your sorting, you know, algorithm efficiency even a little bit, then that can amount to millions of dollars saved for the company. So that essentially, where sorting is very useful down at the more usual level where there isn't all that much data that we're working with, sorting isn't as important but nevertheless, especially in job interviews. interviewers really like asking, you know, sorting questions. Today, we're gonna be going over probably one of the worst algorithms for sorting, but it's also the simplest. So I think the worst would probably be something called random sort, which is just randomly sorting an array and then making and then checking if that sorted. That's probably the least efficient sorting algorithm you can have.

But this is called bubblesort. And it is a lot more efficient than rent sort. But nevertheless, it does, it does still, you know, have some drawbacks. It isn't all that fast. But it's very simple. And it's easy to grasp the concept of it.

And I mean, there's always other sorting algorithms. So if you ever want to learn another one, you could sort of extend the knowledge that you've learned here today to learn that one as well. Anyway, enough talk, let's go and actually create this algorithm. So first of all, let's understand exactly what bubblesort does sort of. So let's say that we have an array 54321, right. So what bubblesort will do is check, start with five, so is five more than four?

It is, so it'll just swap the values. So the result we get four or five. So now we have an array like this. Then it checks is five more than three. It is. So now we do.

So we swapped the values as well. Then I checked is five more than two. It is so we swapped those values. So it's gonna do two, five. And then we check is five more than one it is. So we do 517 as well.

And so we do this then for four until it gets to here. And so now we have four here, and then 231231 here. There we go. So we do that as well. And now check is for more than five. It's not, so it goes to back to two.

So that's usually how bubblesort works. Now, let's implement so we're going to do for int i equals zero, i is less than a dot length, i plus plus, right. So now we're going to do for int i equals one equals zero, i is less than a, I dot length, no, a dot length. I plus plus there we go. All right, so we're going to start with the first value. So, if a i, one is more than a one plus one, then we're going to swap the value.

So how do we solve the values? Well, I one equals I one plus one, and then I one, and then I'm sorry, no, it's gonna be a I one, forgot the brackets, a I one plus one. And then a, I one plus one equals a, I want this present problem, though, doesn't it? So we assign a I one plus one to AI one. When, and now we assign AI one plus one to AI one which is already assigned to AI one plus one, so there's going to be no change. So for that, we need to create an integer temp equals A i one there we go.

Now, one plus one is going to be equal to 10. So there we go. So we have very quickly implemented a sorting algorithm. Now we're going to run it first and then we're actually going to take a look and see exactly how it works in more detail. So let's go to actually print a printing method of printing a loop for as well is less than, less than a dot length. I plus plus.

And then we're going to do System dot out dot print ln. Ay, ay ay. There we go. All right, so now let's run this. Okay, well, okay, I think I made a problem here. Instead of putting I one plus plus I plus plus.

And so in result it was executing forever. Now it should work. All right, well, we got an exception. All right, this is good. This is good. I actually did think about the exception when I was doing it.

But and actually, when I was first creating this sorting method as well, back when I was in college, when I was first just discovering it, I also got this method. So, you know, I, I went right back. Anyway, this method is actually array index out of bounds exception, which just means that we have tried to access an element of the array that doesn't exist. So in this case, we have 01234 elements, right? So since we start counting from zero, there are five elements. So there's four possible Yeah, there's five possible things that we can do from 04.

So maximum four, so the fourth element, so element under the index four is this one. That's difficult to explain. Anyway, one, and so in result, since we have a i plus one here, in result, it's trying to access element six which element Five, the Fifth Element under the fifth index or the sixth element, which doesn't exist. So that to change this, we just have to do a dot length minus one. There we go. And now if you run this, we now get a sorted array.

Beautiful 12345. Now we can go and actually just change up things, we can do 16 712 689 10. And it'll still sort all of it. So 2345 678-910-1216 There we go. So let's take a look at exactly what's happening here. So first of all, we declare our main loop, right, main loop, so we're going to loop a dot length Times.

Then inside that loop, we create another loop. So this loop is going to be for actually checking the value. So essentially, this loop is going to be four we take an element, let's say five. And now we check is More than this one. It is, is it is. So now we swapped them.

So it's gonna be five, four, it's five more than three. It is. So now we do three, five, swap them as well, it's five, more than two, it is to five, and so on, this is going to happen in this loop. Then once that is done, so five in this case is going to be right here, so we check is five more than 16. It's not. So in result, it's going to move on to 16.

So that's going to check is 16. More than seven it is, so it's going to swap them 16 more than 12 more than six more than eight more than nine more than 10. And then finally 60 is going to be at the very end here. And then it'll just go right on back to four. So it's going to end the loop and then go back to four and start with and now it's going to start with three, no four, yeah, from form. And that's going to do that again and again and again, until it finally sorts the entire array.

Okay, so that is essentially sorting arrays. Now, at this point, it may seem a little bit confusing, don't worry about it is I mean sorting arrays is actually an RD like the sort of algorithms like legitimate algorithms. And it can be pretty confusing at first. But after you, you know, you sort of couple of arrays, you learn a little bit, a couple algorithms will start to understand the basics and how it really works. So, yeah, to make it less confusing, we can actually add print statements. So we can now do System dot out dot print ln iterating over plus i.

And then here we can do System dot out dot print ln checking if Ay, ay one plus. is more than plus ay ay one plus one. There we go. All right, there we go. And then we could also add a print statement here system, dot out dot print ln. It is swapping.

And then we do a swapping plus a I one plus and plus a I one plus one. All right, close the print statement. And there we go. All right, and so now If we run this it'll be a little bit less confusing. There we go. So we can now see iterating over zero checking for is more than three, it is swapping four and three.

Genki is four is more checking if four is more than two, it is swapping four. And two. Checking is for the more than fives. It's not checking if is if five is more than 16. It's not checking if 16 is more than seven, it is swapping 16 seven, and then does that goes on and on and on and on. Until finally it gets to the sorted array.

Okay, so good. And do you know get this code running on your machine, read this output. If you're confused really look into the output you can really get the entire the entire idea of the algorithm simply over from this console output that we've created. Here. We're using print statements. If you still can't really understand it, go ahead and take a look at a visual online.

There are tons of visuals, you can actually see where you could actually understand it a little bit better if you really can't understand it from this. Now, the real big problem with a bubble sort algorithm is right here. So you can see now the entire array is sorted. So you can have 2345 678-910-1216, they can see it's sorted, right. And yet, it's still checking and making sure it's still trying to sort it even though it is already sorted. So that's essentially one of the downsides of the bubble sort.

So it takes and that's why it takes a lot more time than some other algorithms. But nevertheless, it is sort of like the beginner sorting algorithm. Alright, so pretty simple. Now let's do some homework. Alright, so for homework this time, it is a little bit tricky. So on the one hand, it's super simple.

On the other hand, you really have to understand what's happening in this algorithm to get it. So you Your job is to sort use the bubble sort algorithm to sort it from highest to lowest instead of from lowest to highest. So before we've been doing it from lowest to highest, so you can see 23456. So your job is to sort it from highest low, so it'll be 16 1210 9876. So 16 will be at the beginning, then 16, then 12, then 10. There's like that.

So just sort of the other way around. Okay, so I wish you luck with homework. Just, if you can understand bubblesort you can understand this homework very, it's very simple. There's pretty much nothing you have to change. So yeah. Anyway, I wish you luck and I'll see you next time.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.