[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Why the fuck do you start with j = 2?

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 12
Thread images: 2

Why the fuck do you start with j = 2?
>>
>>58711152
Because 2 > 1 which makes it way better
>>
>>58711152
Look at the inner loop
>i = j - 1
>>
i is trailing j as it iterates through the loop. when j=2, i =1 and it hits the first element.
>>
This is the correct function
>>
>>58711152
A is 1 indexed and you compare against j-1
>>
>>58711152
A is indexed from 1 to A.length
A[1] is already sorted
>>
>>58711152
Because A[1] is initialized to some pre-computed value which is needed to compute A[2] and so on. However, only values starting at A[2] need to be computed. It's similar to x! = (x-1)!x so you define it as A[1] = 1 A[2] = 1 A[n] = A[n-1] * n.
>>
>reading CLRS
>Arrays are 1-indexed for sorting algorithms
>Arrays are 0-indexed for hash tables
>algorithms textbook co-authored by people who invented RSA
>>
>>58711152
A better question is why the fuck are you bothering with insertion sort? Merge sort and quick sort are the only ones worth mentioning and the reality is that 99.99% of programmers will never have to write a sort algorithm themselves.
>>
>>58716286
Insertion sort is the fastest for small arrays, and any good quicksort algorithm will have a threshold in the partioning step where it does an insertion sort on the remaing bits.
>>
>>58711152
- Because CLRS uses 1-based arrays.
- And you need to compare the jth element to the element before it.
- So, you have to start at j = 2 so you can compare A[j] to A[j-1] (which is A[1])

>>58716286
Maybe he just started reading the book.
Thread posts: 12
Thread images: 2


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.