Coding a Simple Sort Algorithm Visualiser in Java/ Swing

Поделиться
HTML-код
  • Опубликовано: 28 авг 2024

Комментарии • 129

  • @Hopsonn
    @Hopsonn  6 лет назад +60

    In case you are wondering why I didn't talk about "Big O" or whatever, it is just because I don't know too much about it myself and didn't want to misinform anyone :(
    Edit: narrative mistake in the video, bubble sort and insertion sort and selection sort loop through the array up to n^2 times, where n is the number of array elements.
    Somehow I said it loops through it twice :s

    • @xskerrittx1
      @xskerrittx1 6 лет назад +2

      Talk*

    • @user-pu2md8qv5v
      @user-pu2md8qv5v 6 лет назад

      Keep up the great work!

    • @atrumluminarium
      @atrumluminarium 6 лет назад +1

      For anyone watching wanting a quick intuitive explanation, here _n_ is the array size and _O(f(n))_ describes how the algorithm scales with array size

    • @CharIie83
      @CharIie83 6 лет назад +2

      n is the number of calculations necessary. If you have to crosscheck each element with itself, you will have n*n or n^2. It helps suggest a measure of efficiency, and makes it easier to notice how some approaches do a fraction of the tests but yet are equally precise

    • @Hopsonn
      @Hopsonn  6 лет назад +3

      @@CharIie83 yeah that's pretty much it, but I still don't think I would be very good at explaining that in the video lol

  • @HermesPasser
    @HermesPasser 6 лет назад +9

    You editing skills are improving, nice video

  • @mr.unkreativ718
    @mr.unkreativ718 6 лет назад +29

    Insertionsort is O(n^2) not O(2n)! That means you iterate n times over the whole array not only 2 times.

    • @Hopsonn
      @Hopsonn  6 лет назад +4

      Oops, my mistake

  • @wilple
    @wilple 6 лет назад +5

    Nobody mentioning the Donkey Kong Counrtry 2 Aquatic Ambeience in the background.
    Great video and song choice

  • @Protofall
    @Protofall 6 лет назад +28

    Hello, I'm sure you know this and it was just a mistake, but you said stuff like "Bubble sort goes through the array twice" which isn't actually true, it goes through N^2 times (Hence O(N^2))
    Pretty cool video seeing how I've looked at most of these sorting algorithms a few years ago and am now doing a course that uses Swing.
    Clarification edit: N is the number of elements in the array/size of array

    • @Hopsonn
      @Hopsonn  6 лет назад +3

      Yeah you're right, no idea how I made that mistake in the video :s

    • @teckyify
      @teckyify 4 года назад

      No it doesn't, it depends on the input. Average case runtime complexity is O(n^2) but best case where the numbers are close to their sorted location is O(n).
      It's input dependent and can be really quick and cache efficient.

  • @lechi_2002
    @lechi_2002 6 лет назад +35

    It's a shame universities don't teach the newer JavaFX framework. I've used both Swing and JavaFX and I much prefer the latter.

    • @KingEbolt
      @KingEbolt 6 лет назад +6

      JavaFX isn't part of the core JDK anymore as of JDK11

    • @tomb_1343
      @tomb_1343 6 лет назад

      ooo ooo I wonder why that is. Especially considering fx was meant to replace swing.

    • @zlovredniyTip
      @zlovredniyTip 6 лет назад

      Well we were 'taught' (yey for self research) it but I felt it was pretty counter intuitive and docs didnt really help. Have not used swing though so cant say which experience is better :/
      Glad to know that it is not usually the case =)

    • @stephenackerman5707
      @stephenackerman5707 6 лет назад +2

      Swing is absolutely part of JDK 11. See here: download.java.net/java/early_access/jdk11/docs/api/java.desktop/module-summary.html

    • @stephenackerman5707
      @stephenackerman5707 6 лет назад

      Having worked with JavaFX and Swing, I vastly prefer Swing.
      Plus, in the real world, most Java desktop applications are Swing, not JavaFX. JavaFX is very rarely used (in my experience).

  • @ReagueOfRegends
    @ReagueOfRegends 6 лет назад

    Background music gave your video a real Bisqwit vibe. Well done.

  • @AnsityHD
    @AnsityHD 6 лет назад

    Weird, literally just last night I was watching a "15 Sorting Algorithms in 6 minutes" video, did something similar to this.
    Cool to see how it all works :)

  • @theoskille5162
    @theoskille5162 4 года назад

    hey! awesome video! I've been trying to find a way to write a sort algorithm visualizer for awhile now and your solution with the "sleep swap" is really cool! This helped a bunch, thanks

  • @SapphFire
    @SapphFire 6 лет назад +3

    I really like the explanations.

  • @AdamWong
    @AdamWong 5 лет назад

    Very Nice Video Hopson. Nice intro to time complexity.

  • @fnasserebar
    @fnasserebar 6 лет назад +34

    I am missing bogosort :(, Otherwise great video!

    • @steinardarri
      @steinardarri 6 лет назад +21

      Bogosort is best sort. For anyone unfamiliar:
      Bogosort: Shuffle a deck of cards. Ok, is the deck sorted? If not, shuffle it again and check again. Repeat until it is sorted.

    • @stewartzayat7526
      @stewartzayat7526 6 лет назад +12

      Best case scenario, it is the most efficient one, so yes, I would recommend everyone use it.

    • @xtdycxtfuv9353
      @xtdycxtfuv9353 4 года назад +2

      i use bogo sort on gigantic arrays. fight me

  • @shabaq7077
    @shabaq7077 6 лет назад +1

    Just programmed an insertion sort algorithm yesterday 😂

  • @CharIie83
    @CharIie83 6 лет назад

    Very inspiring, I wish I could go to uni and learn this stuff, instead of having to scrounge through youtube vids and stack overflow for quality information

  • @Stray0
    @Stray0 6 лет назад

    selection sort killed it
    damn

  • @36kph
    @36kph 6 лет назад

    sorting algorithms FeelsGoodMan Clap

    • @clarkjarcus
      @clarkjarcus 6 лет назад

      S OMEGALUL RTING ALG OMEGALUL RITHMS

  • @Kitulous
    @Kitulous 6 лет назад +4

    are you familiar with Processing? what can you say about it if you are?

    • @Hopsonn
      @Hopsonn  6 лет назад +11

      Never used it, but I have seen some videos on The Coding Rainbow's channel, looks decent.

    • @iampepepapi
      @iampepepapi 6 лет назад +1

      The Coding Train xD. Good video 🔥

  • @TheDisastermanTv
    @TheDisastermanTv 6 лет назад

    Bubblesort ist the best.
    It really is in the case that you have a huge amount of data, and only some data changes locally.

    • @Dadaskis
      @Dadaskis 6 лет назад

      Bubble sort sucks

  • @pendergastj
    @pendergastj 6 лет назад

    Really cool stuff !

  • @h.hristov
    @h.hristov 6 лет назад

    Thanks for the video!

  • @shinevisionsv
    @shinevisionsv 6 лет назад +1

    Awesome!

  • @Smell-O-Vision-Citrus
    @Smell-O-Vision-Citrus 6 лет назад

    I love this channel.

  • @poppop-oj6by
    @poppop-oj6by 6 лет назад

    I would just save all the steps of the algorithm to a file and then play the animation at a by the user specified speed. I have almost never found a good example where you have to use sleep. In most cases there is another way to do it and it almost always is better. Very nice video though. Some very nice editing and visuals.

    • @Hopsonn
      @Hopsonn  6 лет назад +1

      thanks.
      and I did look into alternative ways (aka not using nanotime, or Thread.sleep like I do since making this video) but I could not find one unfortunately :(

  • @danksamosa3952
    @danksamosa3952 6 лет назад

    Smetana!! Vltava/Moldau, You listen to classical?

  • @Speiger
    @Speiger 6 лет назад

    Cool visualizer, but one question, why do you keep the CPU busy with stuff while sleeping? (I mean a while(sleep) loop is basically just keeping the CPU hat max usage).
    A simple System.sleep(mstosleep) call could have helped too. (But i could be wrong)
    Edit: Anyway also thanks for these kind of things. I never touched that before and its a valueable source to learn from :3

    • @Hopsonn
      @Hopsonn  6 лет назад

      thanks
      and it I am no sure why, but I was getting InterurppedExceptions (Or something) when doing Thread.sleep, so I looked up alternate ways. Since editing that bit, I eventually did change it to Thread.sleep and it works fine now lol

    • @Speiger
      @Speiger 6 лет назад

      lol ^^ Yeah it requires a Try catch around it to be save.
      But yay.

  • @KingEbolt
    @KingEbolt 6 лет назад

    Dammit Hopson, I was making this and you just beat me to it :(

  • @teckyify
    @teckyify 4 года назад

    BubbleSort is NOT useless. First of all you must check if during a run a swap happened an if not stop (there are other implementations). If the numbers you are sorting are almost at the right location you get away with a runtime of O(n). The runtime of BubbleSort can best be illustrated by locating the smallest number in the array. The farther it's close to the end the more quadratic the runtime is, because it needs one complete run to move the number one step to the left side of the array.

  • @alexandrajane7843
    @alexandrajane7843 5 лет назад

    Hello, very good codes i love it. But please is there a way to print the time elapsed by each sorting algorithms? And also to see the different random numbers that are sorted on the graph?

  • @nipplesniper
    @nipplesniper 2 года назад

    Hello :) Read your code, though I cant see how you handled resizing events. I also want the bars to take the whole width of the application everytime it is resized.

  • @tundrummax6221
    @tundrummax6221 5 лет назад

    Could one of these algorithms be used for preventing 3D polygons from overlapping?

  • @saintpreacher3973
    @saintpreacher3973 3 года назад

    It doesnt repaint when i swap, what can be the issue?

  • @anonofDeath
    @anonofDeath 3 года назад

    How'd you think of the sleepfor method? Everywhere online, people always spam "to use a swing timer." Did you watch any tutorial or anything to figure it out?

    • @Hopsonn
      @Hopsonn  3 года назад

      Don't remember, but looking at it now, nearly 3 years on, it is far from an ideal solution as it is a spinlock. This means it just loops until some condition is met, which wastes CPU time.
      A much better solution would be to sleep the thread
      www.journaldev.com/1020/thread-sleep-java#:~:text=sleep%20in%20Java-,Thread.,negative%2C%20else%20it%20throws%20IllegalArgumentException%20.

    • @anonofDeath
      @anonofDeath 3 года назад

      @@Hopsonn thank you so much! replying to a comment on an old video shows how much dedication you put on your youtube channel. Thank you!!!

    • @anonofDeath
      @anonofDeath 3 года назад

      @@Hopsonn I have a question if you don't mind answering. When ever I use thread.sleep, my screen goes white. It's probably because it's using the same thread to display it. How did you get around it, if you do remember. Thank you for your time.

  • @Sisyphus.
    @Sisyphus. 2 года назад

    does anyone have a guide or a tutorial for this project

  • @Strikker1918BAR
    @Strikker1918BAR 4 года назад

    W0rthy's array vizulizer can sound painful sometimes

  • @PetahSchwetah
    @PetahSchwetah 6 лет назад

    Do Bogo sort and Miracle sort ;)

  • @Ivan-qb7kc
    @Ivan-qb7kc 6 лет назад +1

    Not Rust? (((

  • @shameekagarwal4872
    @shameekagarwal4872 4 года назад

    please please release a tutorial for building this..

  • @pauribelles7920
    @pauribelles7920 6 лет назад +1

    Should I stop learning C++ as my main language?

    • @Dadaskis
      @Dadaskis 6 лет назад +4

      Every language have pluses and minuses, and every language is tool. Use correct tool in correct problem.

    • @michaelzing1197
      @michaelzing1197 6 лет назад +3

      nah, C++ is still widely popular (if it ins't the most popular), you should still learn other languages

    • @leleo53000
      @leleo53000 6 лет назад +3

      depends.
      C++ and Java have quite opposite philosophy:
      C++ is fully compiled, and is going to do everything for those sweet performances. It has a very basic standard library, but tons of library. It's not as cross-platform as Java, but it's only getting better and better.
      Java is more about patterns and high level abstractions. It's quite slow to be honest, but only game developers care. It has a huge standard library, wich mean good stuff out of the box, but also means loads of long dead modules trapping new devs.
      I gotta admit, I'm not a big fan of Java, and I'm a 100% biased toward C++, so take it in account ...
      Overall, I do believe that learning C++ is a very good experiences : it makes you think about what is actually happening on your computer when you code, nothing is "hidden away". On the other hand, great powers implies great responsibilities, and it's easy to get into old C unending mess of weird pointers: if you learn C++, you should definetly keep up to date with the last updates and the good practices (auto, smart pointers, iterators, explicit override, move vs copy, etc. )
      Note: my english is probably a bit weird, hope you don't mind

    • @paulweerheim
      @paulweerheim 6 лет назад

      @@leleo53000 but Java is incredibly fast for what it does. Way faster than most GC enabled languages which run in a virtual environment. However, I like how you explain languages. I think languages are tools, if you only know one language, and cant switch to another rather quickly, then you probably aren't that much of a programmer.

    • @ryuuji159
      @ryuuji159 6 лет назад

      @@leleo53000 Since my first language was Java I always miss things of the standard libraries of most languages, the big one is always the gui

  • @Noe-Mastro
    @Noe-Mastro 6 лет назад

    Please make a new video on ASCIIMON !

  • @kim15742
    @kim15742 6 лет назад

    lol, Molau in the background

  • @AlexVasiluta
    @AlexVasiluta 6 лет назад

    Nice

  • @li-on1
    @li-on1 6 лет назад

    So do you learn Java swing?
    We just got told:
    Use this.. thats it xD
    They didn't even tell us the name of the framework at my university ._.

  • @ffelixpe6860
    @ffelixpe6860 6 лет назад

    What text editor and compiler did you use?

    • @Hopsonn
      @Hopsonn  6 лет назад

      Netbeans ide and oracle jdk

    • @ffelixpe6860
      @ffelixpe6860 6 лет назад

      Hopson thanks, you inspires me

  • @PythonPlusPlus
    @PythonPlusPlus 6 лет назад

    What was the sorting algorithm just before the video ended?

    • @Hopsonn
      @Hopsonn  6 лет назад +3

      first one was merge, second was quick sort

    • @PythonPlusPlus
      @PythonPlusPlus 6 лет назад

      Hopson That was soo fast, if you blink you miss it.

  • @superblaubeere27
    @superblaubeere27 5 лет назад

    Why did you not use LWJGL? (The API of Minecraft)

  • @superblaubeere27
    @superblaubeere27 5 лет назад

    Why are you using Netbrains instead of IntelliJ

  • @Krumm3L
    @Krumm3L 6 лет назад

    So, what do you need this for? i didnt really understood it

    • @maxjordan3009
      @maxjordan3009 6 лет назад

      Say you had a data set that you wanted to search often. It would be better to use a binary search on this as it is much quicker. However to use a binary search you need to have the data sorted, this is where you would use a sorting algorithm. You send all the data through the algorithm and what comes out the other side is your data in the correct order.

    • @Hopsonn
      @Hopsonn  6 лет назад +1

      You probably use sorting algorithms all the time without realising it.
      A common example would be online shopping, where you can sort the products by price low to high. This would be done using sorting algorithms.

    • @Krumm3L
      @Krumm3L 6 лет назад

      Well, didnt knew that they do that with an sorting algorithm

    • @KingEbolt
      @KingEbolt 6 лет назад

      @@maxjordan3009 I guess if you are doing a search often you would sort the list and then use binary search, but if you are not doing that many searches it would be better to just do linear search on a non sorted list because the big o would be O(n), compared to sorting which would be O(nlogn) + O(logn) which simplifies to O(nlogn) which is less efficient.

    • @maxjordan3009
      @maxjordan3009 6 лет назад

      Yeah thats true, was just the first example I could think off from the top of my head.

  • @askecc
    @askecc 6 лет назад

    How did you learn to program?

    • @Hopsonn
      @Hopsonn  6 лет назад

      ruclips.net/video/0Sk76NZcd84/видео.html

  • @redmoney119
    @redmoney119 3 года назад

    Is it possible to create a web with vizualuzs algorithms?

    • @Hopsonn
      @Hopsonn  3 года назад +1

      It is, you can use JavaScript with the canvas API
      developer.mozilla.org/en-US/docs/Web/API/Canvas_API

    • @Hopsonn
      @Hopsonn  3 года назад +1

      developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial

    • @redmoney119
      @redmoney119 3 года назад

      @@Hopsonn yayayay thank u....

  • @Pedro_ZKS
    @Pedro_ZKS 5 лет назад +1

    """Simple""", at least for me

  • @askecc
    @askecc 6 лет назад

    Did you go to some university, and if you did, where?

  • @MrOnlineCoder
    @MrOnlineCoder 6 лет назад

    Why you didn't use c++ and sfml?

    • @Hopsonn
      @Hopsonn  6 лет назад

      As said in the video, I wanted to practice using Swing for university :P

  • @MiSt3300
    @MiSt3300 6 лет назад

    Hello~!

  • @meech37
    @meech37 2 года назад

    it would be nice if you didn't have the entire first part on like x25 speed lmao

  • @lorenzoorlando311
    @lorenzoorlando311 4 года назад

    Hi guys, i'm having trouble with this:
    stackoverflow.com/questions/62079995/sorting-visualization-with-quick-sort

  • @ariseyhun2085
    @ariseyhun2085 6 лет назад

    hello

  • @thearmyofiron
    @thearmyofiron 6 лет назад

    Post bogosort

  • @liquidexw
    @liquidexw 6 лет назад

    why don't you use a better IDE, like IntelliJ IDEA? NetBeans is really old and outdated.

    • @Protofall
      @Protofall 6 лет назад +1

      Nah mate, Notepad is where its at ;)

    • @Hopsonn
      @Hopsonn  6 лет назад +4

      Why don't use a better IDE, like Netbeans? IntellJ is really slow and takes 30 minutes to load up.

    • @Hopsonn
      @Hopsonn  6 лет назад +5

      But yeah, I have tried IntelliJ and didn't like it.
      Saying it is "a better IDE" is subjective, and not fact. People use what they prefer, I just happen to prefer netbeans.

    • @liquidexw
      @liquidexw 6 лет назад

      @@Hopsonn Well I don't know what disk drive do you use, or what else slows down your launch time, mine loads in like 5-10 seconds, both on Windows and Ubuntu. Also do note that you don't close and reopen an IDE constantly, unlike a text editor (which is a valid reason not to use Atom lol)
      As for your second reply: I'm just recommending IntelliJ based on my own experience. I liked the inspections, code completion, it just helps in making your code better.

    • @Hopsonn
      @Hopsonn  6 лет назад +2

      I was joking lol, you had some silly comments on netbeans, so i gave silly comments on intellij

  • @dustyhob0
    @dustyhob0 6 лет назад

    S O R T I N G A L G O R O T H M

  • @knightlon
    @knightlon 6 лет назад

    Merge sort is far from the best.

  • @RossLegs
    @RossLegs 6 лет назад

    A

  • @LemonChieff
    @LemonChieff 5 лет назад

    Java makes me sad :(

  • @rRT_1
    @rRT_1 6 лет назад +2

    damn that takes too much logic i think front end web development is enough for me

    • @paulweerheim
      @paulweerheim 6 лет назад +2

      Its called boilerplate. And yes you're right. It gives you the ability the create efficient code though.

  • @ExtremeDev
    @ExtremeDev 6 лет назад

    netbeans? really bro?

    • @Hopsonn
      @Hopsonn  6 лет назад

      Yes. Problem?

    • @ExtremeDev
      @ExtremeDev 6 лет назад

      why not intellij?

    • @Hopsonn
      @Hopsonn  6 лет назад

      because i prefer netbeans

    • @ExtremeDev
      @ExtremeDev 6 лет назад

      netbeans is too gay

  • @deepakpandey9406
    @deepakpandey9406 5 лет назад

    It'd have been good if you have explained the video slowly, only just typing code 🤮

  • @Pedro_ZKS
    @Pedro_ZKS 5 лет назад

    """Simple"""