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
€65.24
List Price:  €93.20
You save:  €27.96
£55.93
List Price:  £79.91
You save:  £23.97
CA$95.58
List Price:  CA$136.56
You save:  CA$40.97
A$107.13
List Price:  A$153.05
You save:  A$45.92
S$95.25
List Price:  S$136.08
You save:  S$40.82
HK$547.90
List Price:  HK$782.75
You save:  HK$234.85
CHF 63.85
List Price:  CHF 91.21
You save:  CHF 27.36
NOK kr770.13
List Price:  NOK kr1,100.23
You save:  NOK kr330.10
DKK kr486.51
List Price:  DKK kr695.05
You save:  DKK kr208.53
NZ$117.65
List Price:  NZ$168.07
You save:  NZ$50.42
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,679.19
List Price:  ৳10,970.75
You save:  ৳3,291.55
₹5,834.32
List Price:  ₹8,335.10
You save:  ₹2,500.78
RM333.81
List Price:  RM476.90
You save:  RM143.08
₦91,422.33
List Price:  ₦130,608.93
You save:  ₦39,186.60
₨19,487.31
List Price:  ₨27,840.21
You save:  ₨8,352.89
฿2,585.90
List Price:  ฿3,694.31
You save:  ฿1,108.40
₺2,277.50
List Price:  ₺3,253.72
You save:  ₺976.21
B$361.10
List Price:  B$515.88
You save:  B$154.78
R1,320.17
List Price:  R1,886.05
You save:  R565.87
Лв127.48
List Price:  Лв182.13
You save:  Лв54.64
₩96,293.43
List Price:  ₩137,567.94
You save:  ₩41,274.51
₪266.53
List Price:  ₪380.78
You save:  ₪114.24
₱4,038.21
List Price:  ₱5,769.12
You save:  ₱1,730.90
¥10,975.59
List Price:  ¥15,680.08
You save:  ¥4,704.49
MX$1,206.90
List Price:  MX$1,724.22
You save:  MX$517.31
QR255.26
List Price:  QR364.68
You save:  QR109.41
P965.80
List Price:  P1,379.77
You save:  P413.97
KSh9,448.65
List Price:  KSh13,498.65
You save:  KSh4,050
E£3,352.63
List Price:  E£4,789.68
You save:  E£1,437.04
ብር3,985.10
List Price:  ብር5,693.24
You save:  ብር1,708.14
Kz58,489.70
List Price:  Kz83,560.30
You save:  Kz25,070.60
CLP$66,385.51
List Price:  CLP$94,840.51
You save:  CLP$28,455
CN¥507.19
List Price:  CN¥724.59
You save:  CN¥217.40
RD$4,111.01
List Price:  RD$5,873.12
You save:  RD$1,762.11
DA9,398.61
List Price:  DA13,427.17
You save:  DA4,028.55
FJ$158.17
List Price:  FJ$225.97
You save:  FJ$67.79
Q544.39
List Price:  Q777.73
You save:  Q233.34
GY$14,639.41
List Price:  GY$20,914.34
You save:  GY$6,274.92
ISK kr9,791.60
List Price:  ISK kr13,988.60
You save:  ISK kr4,197
DH708.11
List Price:  DH1,011.63
You save:  DH303.52
L1,243.72
List Price:  L1,776.81
You save:  L533.09
ден4,016.98
List Price:  ден5,738.79
You save:  ден1,721.81
MOP$563.94
List Price:  MOP$805.67
You save:  MOP$241.72
N$1,340.22
List Price:  N$1,914.68
You save:  N$574.46
C$2,575.16
List Price:  C$3,678.96
You save:  C$1,103.79
रु9,316.74
List Price:  रु13,310.19
You save:  रु3,993.45
S/261.07
List Price:  S/372.97
You save:  S/111.90
K269.52
List Price:  K385.04
You save:  K115.52
SAR262.50
List Price:  SAR375.02
You save:  SAR112.51
ZK1,842.03
List Price:  ZK2,631.59
You save:  ZK789.55
L324.63
List Price:  L463.78
You save:  L139.14
Kč1,640.70
List Price:  Kč2,343.96
You save:  Kč703.26
Ft25,605.88
List Price:  Ft36,581.40
You save:  Ft10,975.51
SEK kr762.91
List Price:  SEK kr1,089.92
You save:  SEK kr327.01
ARS$61,153.48
List Price:  ARS$87,365.86
You save:  ARS$26,212.38
Bs484.56
List Price:  Bs692.26
You save:  Bs207.70
COP$275,828.59
List Price:  COP$394,057.74
You save:  COP$118,229.14
₡35,172.98
List Price:  ₡50,249.28
You save:  ₡15,076.29
L1,727.65
List Price:  L2,468.18
You save:  L740.53
₲519,796.34
List Price:  ₲742,598.03
You save:  ₲222,801.68
$U2,684.12
List Price:  $U3,834.62
You save:  $U1,150.50
zł281.95
List Price:  zł402.81
You save:  zł120.85
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.