Placeholder Image

Subtitles section Play video

  • When we originally started talking about the sorting problem, we described our input as a list of integers.

  • So I want to go back and I want to do another implementation where our input is truly something that we would consider to be a list.

  • There are a couple of reasons we're doing this, partly it's because of the next thing we're going to look at, which is trying to prove that it's actually correct.

  • Okay, so let's go back, we'll go through sorting the exact same data we had before.

  • Whoops, my debugging duck is in the way.

  • So currently we don't have any data, I guess we could consider that to be completely sorted already.

  • So let's go back and look at the same sort of data set that we had before.

  • Okay, this time I'm going to assume that this is a list.

  • I'm going to separate my numbers just to indicate that I don't really mean for this to be an Just like before, we're going to work through from left to right to find our candidate for the lowest value, the minimum.

  • Two looks pretty good, I get to 34, I get to 1, that's a better candidate than 2. 34, 3, 89, 13, 55.

  • Now here's where we're going to depart from our previous approach.

  • Instead of swapping elements around, I'm going to literally remove an element from our list, and our list shrinks down to a smaller size.

  • And we append the element to a new list.

  • So I'm going to go through the process again, 2 is a pretty good candidate, 34, 3, 89, 13, 55, none of those are better than 2, so I remove 2 from my list.

  • The list gets a little bit smaller, 2 gets appended to my solution list.

  • Repeat the process again, 34 is pretty good until I see 3, and then 89, 13, and 55, 3 gets removed from my list and appended to my solution list.

  • Repeat the process again, 34 seems pretty good, it's definitely better than 89, but 13 is better than 34, 55 isn't any better than 13, so we remove the 13, shrink our list down, and append our 13 to our solution.

  • And we continue this process again, and again, until the last item is removed.

  • And just like before, we've added all of our data to our list in order from smallest to largest, the data is completely sorted.

  • One of the real big distinctions here, however, is that we destroyed our initial list and created a new list instead of swapping things around in place.

  • Okay, so as a programming review, let's look at some code that's going to implement this.

  • So just like before, I've got an array of values that represents my data, and this time I'm going to put it in an array list.

  • So it's technically a list, but it does have an array to kind of support or hold the data.

  • So we'll look at this more in depth this week.

  • So our overall goal is to have two different lists, one that represents our input values that's already been constructed, and another one that will be our sorted data.

  • So I'm going to create an array that represents the sorted data, and it'll be initially an empty array list, a brand new array list.

  • Then I'm going to prototype how I'm going to use this new method.

  • Okay, so I'm going to call my selection sort, and I'm going to provide it with these two lists, the input list and the sorted list.

  • Okay, then when I'm done, I'd like to print out my list to make sure that it is in fact sorted.

  • Okay, so I've kind of set up my problem.

  • It's time to go and create a method.

  • So it's going to be a public static method with a void return value, and it's going to have those two parameters, the two lists, the list of input and the list that will represent the sorted data.

  • Okay, so now we have to figure out how to solve our problem.

  • So just like when I was working with this on the table, I'm going to process data while there's still data in the input.

  • And basically what I have to do is find the minimum value in that input.

  • So I'm using a counting for loop here.

  • By the way, this has some subtle side effects that we'll look at in a little bit.

  • But just like before, I'm creating a variable to keep track of where I found my best value, and I'm going to go through and compare each value to the best one I found so far.

  • And if it happens to be a smaller value, I will update where I think the location of my best value overall is.

  • Okay, so when I'm all done with the loop, I've found the minimum values location.

  • So now I can get the minimum value, and I need to add it on to my sorted list.

  • Okay, now that I've added on to the sorted list, I might as well remove it from the input list.

  • Okay, and that's it.

  • Let's run it and test it and see if it sorts our little sample list.

  • And in fact, it did.

  • Okay, now I'm going to do something additional here.

  • I'm going to go through a process called instrumentation.

  • So the idea of instrumentation is augmenting code in various ways to track performance and gather data about it.

  • In this case, it's going to be a really, really, really crude form of instrumentation.

  • I'm just going to add in some additional print messages, and we'll try and use those to get a sense of how long it takes this code to run.

  • So I'm going to put a print message before I do my sorting that says I'm starting.

  • I'm going to put another print message after I do my sort that says we're done.

  • Okay, then I'm going to run my program, and it printed everything almost instantaneously.

  • There seemed to be no significant difference in time between when it started and when it finished.

  • So I'm going to go up, and I'm going to change the data that I'm supplying.

  • And so instead of supplying it just a little bit of data, I'm going to change this for loop.

  • And I'm just going to append a whole bunch of data.

  • So instead of using my previous values, I'm just going to generate random integers.

  • So I'm going to be using math.random here.

  • I know that math.random gives me a double number, and I want to end up with an int at the end.

  • So I'm going to do some typecasting.

  • Basically, what this is doing is it will give me a random integer between zero and the maximum possible integer value.

  • Then I'm going to change my loop.

  • I'm just going to try and have it count up to 10,000.

  • I'm using a notation you can use in newer versions of Java.

  • Oh, wow, that was quick.

  • So it sorted 10,000 items really, really quick.

  • Let's try 20,000.

  • That was pretty quick, too.

  • Let's try 30,000, maybe.

  • One, two.

  • Okay, so that was maybe a second.

  • That was at least noticeable, right?

  • 40,000.

  • One, two.

  • Pretty quick, too.

  • But about two seconds, maybe.

  • Two to three seconds for 40,000 items.

  • Now, that was on my computer.

  • My computer is several years old.

  • Your computer might be faster, so maybe they'll run quicker for you.

  • Or maybe your computer is a different model with a different processor, and it'll run slower.

  • So this is an empirical test.

  • It's an experiment.

  • I was measuring how long it would take.

  • Now, let's look at something interesting.

  • So I used an ArrayList.

  • But an ArrayList and a LinkedList are somewhat interchangeable.

  • I mean, they've got the same operations.

  • So let's see what happens if we do a search and replace, and replace the ArrayList with a LinkedList.

  • So I'm going to search for all of the places where ArrayList is used.

  • And we'll replace it with LinkedList.

  • OK.

  • Now, before I get carried away, I think I'm going to back down.

  • I'm going to test out my simple original sample problem.

  • So I'm going to put in the loop that uses my original test values there.

  • I'll run it with those values.

  • Oh, yeah.

  • OK, that's cool.

  • So it ran, sorted my six or so values there.

  • It looks good.

  • OK, seven values.

  • Let's go back and try it again with more data.

  • So I'm going to put my loop back in.

  • I'm going to set it at 10,000.

  • One, two, three.

  • Wow, if you remember, the ArrayList version took about two seconds.

  • But this is taking forever.

  • OK, so a very subtle change there had a huge impact on performance.

  • Huge.

  • I mean, we're still waiting for it to finish.

  • Now, this does not mean LinkedLists are bad.

  • LinkedLists have some really great strengths.

  • But they've got some weaknesses, too.

  • And I have used a LinkedList in a poor way.

  • I could restructure this in ways where a LinkedList would perform just as well as the ArrayList did.

  • So this is one of the big takeaways from this class, understanding all of these subtle distinctions and how just a subtle change can have a huge impact on overall program performance.

  • Thank you.

When we originally started talking about the sorting problem, we described our input as a list of integers.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it

A2 US list sorted data input array loop

0.5 Data Structures & Algorithms: List-Based Selection Sort

  • 51771 1666
    zack posted on 2025/01/06
Video vocabulary

Keywords

sort

US /sɔrt/

UK /sɔ:t/

  • verb
  • To arrange things in a systematic way, typically into groups.
  • To arrange things in groups according to type.
  • To organize things by putting them into groups
  • To deal with things in an organized way
  • noun
  • A category of things or people with a common feature; a type.
  • Group or class of similar things or people
process

US /ˈprɑsˌɛs, ˈproˌsɛs/

UK /prə'ses/

  • verb
  • To organize and use data in a computer
  • To deal with official forms in the way required
  • To prepare by treating something in a certain way
  • To adopt a set of actions that produce a result
  • To convert by putting something through a machine
  • noun
  • A series of actions or steps taken in order to achieve a particular end.
  • A summons or writ to appear in court or before a judicial officer.
  • A systematic series of actions directed to some end
  • Dealing with official forms in the way required
  • Set of changes that occur slowly and naturally
  • A series of actions or steps taken in order to achieve a particular end.
  • other
  • To perform a series of operations on (data) by a computer.
  • To deal with (something) according to a particular procedure.
  • Deal with (something) according to a set procedure.
  • To perform a series of mechanical or chemical operations on (something) in order to change or preserve it.
  • To perform a series of mechanical or chemical operations on (something) in order to change or preserve it.
  • Take (something) into the mind and understand it fully.
  • other
  • Deal with (something, especially unpleasant or difficult) psychologically in order to come to terms with it.
subtle

US /ˈsʌtl/

UK /'sʌtl/

  • adjective
  • Delicate or slight so it is difficult to perceive
  • Clever or indirect but hides the true purpose
basically

US /ˈbesɪkəli,-kli/

UK /ˈbeɪsɪkli/

  • adverb
  • Used before you explain something simply, clearly
  • Used as a filler word or discourse marker, often to indicate a summary or simplification.
  • In the most important respects; fundamentally.
  • In essence; when you consider the most important aspects of something.
  • Primarily; for the most part.
  • In a simple and straightforward manner; simply.
impact

US /ˈɪmˌpækt/

UK /'ɪmpækt/

  • noun
  • A striking effect or result to hit with force
  • Act or force of one thing hitting something else
  • A marked effect or influence.
  • other
  • To collide forcefully with something.
  • verb
  • To hit or strike someone or something with force
  • other
  • (especially of a tooth) wedged so that it cannot erupt.
  • To have a strong effect on someone or something.
completely

US /kəmˈpliːtli/

UK /kəmˈpli:tli/

  • adverb
  • In every way or as much as possible; totally.
  • In every way or as much as possible
  • Totally; entirely.
  • To the greatest extent; thoroughly.
  • In every way or as much as possible; totally.
  • Including all or everything; without anything lacking.
  • Thoroughly; to a full or finished extent.
  • Totally; in every way or as much as possible.
random

US /ˈrændəm/

UK /'rændəm/

  • adjective
  • Chosen, done without a particular plan or pattern
represent

US /ˌrɛprɪˈzɛnt/

UK /ˌreprɪ'zent/

  • other
  • To act on behalf of someone in a formal setting.
  • To depict or portray something in a work of art.
  • To stand for or symbolize something.
  • verb
  • To depict art objects, figures, scenes; to portray
  • To show or describe something in a particular way
  • To act on behalf of others in government
  • To act or speak for another person or other people
previous

US /ˈpriviəs/

UK /ˈpri:viəs/

  • adjective
  • Coming or occurring before something else in time or order.
  • Existing or occurring immediately before in time or order.
  • Existing or happening before the present time.
  • Existing or happening before the present time
  • Existing or occurring before in time or order.
  • Having occurred or existed before.
  • Immediately preceding in time or order.
  • Immediately preceding in time or order.
  • Coming or occurring before something else; preceding.
  • noun
  • A button or link that allows navigation to a preceding item or page.
  • adverb
  • Before; previously.
track

US /træk/

UK /træk/

  • verb
  • To use marks to follow a wild animal
  • To move a certain way/follow a particular course
  • To record and examine the progress of something
  • To follow the trail or movements of someone or something.
  • To monitor or record the progress or development of something.
  • noun
  • A prepared course for racing, especially for athletes.
  • A circular course for running
  • A circular path on a magnetic disk or tape on which data can be recorded.
  • Course or way someone takes, e.g. in education
  • A mark or impression left by a moving object.
  • A recording of a song or piece of music.
  • A recording of a song or piece of music.
  • A rough path or minor road.
  • The rails on which a train runs.
  • The rails on which a train runs.
  • A prepared course for racing.
  • Path in a field or a forest made by walkers
  • Often circular course laid out for car racing
  • One of multiple musical recordings on an album
  • Band surrounding the wheels of a tank
  • Metal lines that trains ride on
  • One of the rails making up a railway line.
  • other
  • To follow the trail or movements of someone or something.