Algorithms for Work: Array - Rules and Ideas

Algorithms for Work: Array - Rules and Ideas

tl;dr

  • Why do we need to sort? To give rules to array ~> to take advantage of rules to reduce the search range.
  • The index can be more than just an index.
  • The quiz is in the end, try to solve it


Why do we need to sort? Let see.

Array stores many pieces of data with the same datatype and gives you access to them. For example: 7, 2, 6, 4, 11, 23, 5, 1, 5.

It's fine, but every data structure comes with CRUD. You want to search for a value and check whether it exists or not, if yes, how many time.

To find the number 5, you need to go through all the array till you meet it and have no way to make sure you can stop.

To solve this problem, we want to somehow reduce the searching range. We don't want to go through all the array. That's why we want to give the array a set of rules.

We sort it to: 1, 2, 4, 5, 5, 6, 7, 11, 23

Now we can check 1, 2, 4, 5, 5, 6 and stop here, don't need to go further. Why?

Cuz we know that we never find 5 from this point on, every have from this point is greater than 5. That's the set of rules we made and then used.

Same with if you find 3, we just check 1, 2, 4 and stop with the same idea.

You can have any kind of rule you want, but let's use sort for now.


But this is still slow somehow...

They want to make it faster. Why is that? Because if the worst case, we still have to go through all the array if the value we need to find is in the end of the list.

So we have binary search.

The idea of binary search

The idea is to pick a number in the middle of the current list.

Compare this value to the value we are looking for AND we know where the result will be in (by focusing on the side that can find result and ignore the side that the result can't be in)

Same of run of binary search

We know that it's O(logn) and O(n) time complexity when compare those 2 solutions. And please also see this solution from the POV: "We reduce the search range after every check by half."

This idea can apply to any problem you face in the future,

"Find the rules for it and try to use them to reduce the search range."


To test your solution let come with a quiz

The balance scale

You have 16 gold coins. And one of the coins is fake and has less weight than the real one. Let use this balance scale to find out the fake one.

How many times do you need to use it to 100% confirm that you find the fake coin on every try?

For me, it can be 15 times. I pick 1 coin and put it on the left side, and then try 15 remaining coins on the right side, the worst case, I pick the fake coin last and take it 15 times.

The solution will come in next article, and we will talk about index on that part 2

Tuan Nguy?n

??Developer at Japfa Comfeed Vietnam

11 个月

Waiting for pt 2 :D

回复
Nam Nguyen

? Chia s? v? nh?ng ?i?u mình bi?t, nh?ng sai l?m ?? g?p ph?i và gi?i pháp lu?n :D Coaching 1:1 thu?t toán và mindset t? duy Whatever you are not changing, you are choosing Let's connect!

11 个月

Can you comment your number you need? 15? 8?

回复

要查看或添加评论,请登录

Nam Nguyen的更多文章

  • Weekly NewsLetter: ?i làm kh?ng vì ti?n

    Weekly NewsLetter: ?i làm kh?ng vì ti?n

    ?i Làm Kh?ng Vì Ti?n – M?t Cau Chuy?n Ngh?ch Ly ?ay là m?t tiêu ?? gay tò mò. ??c ??n cu?i, b?n s? th?y nó th?t s? ?úng…

    33 条评论
  • Weekly Newsletter: Th?i h?c 1 l?n dùng 1 ??i ?? h?t?

    Weekly Newsletter: Th?i h?c 1 l?n dùng 1 ??i ?? h?t?

    S? h?c Trong l?n ng?i chia s? v?i các anh, mình có nghe ???c m?t cau ?úc rút r?t hay. Th?i x?a các c? h?c 1 l?n ?? dùng…

    18 条评论
  • Weekly Newsletter: Relearn | Unlearn

    Weekly Newsletter: Relearn | Unlearn

    V? tay nhanh nh?t th? gi?i G?n ?ay mình ?i h?c nói m?i nh? l?i m?t video t? x?a: K? l?c v? tay nhanh nh?t th? gi?i…

  • Weekly NewsLetter: C?m ?n nh?ng l?i nh?n xét

    Weekly NewsLetter: C?m ?n nh?ng l?i nh?n xét

    Bài h?m nay mu?n nói v? hành trình mình ?i tìm nh?ng l?i nh?n xét ti?p theo ?? thay ??i b?n than mình t?t h?n. Ph?n ??u…

    4 条评论
  • Weekly NewsLetter: L?a ch?n ? tu?i 30 - Th?c t?

    Weekly NewsLetter: L?a ch?n ? tu?i 30 - Th?c t?

    ??u tiên là quay l?i Part 1: 4 ni?m tin c? s? 1 chút cho t? duy này 1. Cu?c ??i là m?t chu?i các l?a ch?n.

    20 条评论
  • Weekly NewsLetter: L?a ch?n ? tu?i 30 Part 2

    Weekly NewsLetter: L?a ch?n ? tu?i 30 Part 2

    N?u ph?i ?úc k?t c? qu?ng th?i gian mình h?c h?i và tìm cho mình ???c các b?n ch?t c?a v?n ?? thì th?t s? r?t dài…

    24 条评论
  • L?a ch?n tu?i 30 - Part 1 Ni?m tin

    L?a ch?n tu?i 30 - Part 1 Ni?m tin

    Ni?m tin s? 1 Cu?c ??i là m?t chu?i các l?a ch?n. Cu?c s?ng c?a m?i ng??i th?c ch?t là k?t qu? c?a nh?ng l?a ch?n chúng…

    8 条评论
  • Weekly NewsLetter: IT H?c nói P1: H?t h?i

    Weekly NewsLetter: IT H?c nói P1: H?t h?i

    Góc nhìn t? podcast v?i Tr?n Qu?c Huy H?m v?a r?i, khi tham gia podcast cùng anh Tr?n Qu?c Huy , mình nh?n ra r?ng c?ng…

    20 条评论
  • PDCA - Plan Do Check Act - H?c 1 bi?t nhi?u

    PDCA - Plan Do Check Act - H?c 1 bi?t nhi?u

    Trong chu?i bài vi?t n?u t?i bi?t tr??c khi 30, th?c ra ?i?u này v? ly thuy?t thì mình bi?t r?i, nh?ng nó l?i chính là…

    1 条评论
  • MVP Hành trình h?c nói - Step 7/7

    MVP Hành trình h?c nói - Step 7/7

    Xin chào mn 7 b??c rèn luy?n t? duy nói chia làm 3 ph?n chính là: Ti?p nh?n ki?n th?c ??nh ngh?a L?a ch?n th?ng tin…

    7 条评论

社区洞察

其他会员也浏览了