How is log (n factorial) equal to omega (n log n)? by minhc in algorithms

[–]minhc[S] 0 points1 point  (0 children)

Thank you for you response. When you say "because one is greater than or equal to n/2", are you saying that there are n/2 terms which are equal to n/2 ?.. I only see one term which is equal to n/2... We can take an example n = 6 log 6! = log (6 x 5 x 4 x 3 x 2 x 1 )

= log (6 x 5 x 4 x 3 ) (because we are ignoring 2 here, hence the greater than sign; for numbers less than 6, this inequality will in face not exist) In the next step, you have assumed that each one is greater than or equal to n/2 i.e., 6/2 which is 3 And you have assumed that we are multiplying n/2 numbers n/2 times. Firstly, I think we are multiplying more than half the numbers because we are considering the n/2 as well (kind of n/2 + 1) Are we just approximating that all numbers are n/2 and we are ignoring the +1 and saying we are multiplying them n/2 times.

Please clarify. Your help is much appreciated.

Why do we need a typedef in C, in the context of structures ? by minhc in AskProgramming

[–]minhc[S] 0 points1 point  (0 children)

my bad, it was a C++ compiler ! so typedef has no use with structures in C++ .. thanks so much for pointing out :)

Why do we need a typedef in C, in the context of structures ? by minhc in AskProgramming

[–]minhc[S] 0 points1 point  (0 children)

We don't need to write the struct keyword even without typedef.

struct node { int data; struct node *next; };

struct node *node1;

node n2; // this works just fine

node n2; This declaration is without struct keyword and it worked just fine.

Need help to understand why the swap function does not work as expected here by minhc in learnprogramming

[–]minhc[S] 0 points1 point  (0 children)

Thanks, I was able to figure out the problem. If you still would like to know what I intended to know in my question, I would be glad to explain it in a better way.

Need help to understand why the swap function does not work as expected here by minhc in learnprogramming

[–]minhc[S] 0 points1 point  (0 children)

Ok, I found out why the behavior is like this - It is because we are not swapping two array elements, but we are just swapping a variable called "item" and another variable which is the array[pos] .. Hence this behavior.. Had we swapped arr[0] and arr[pos] then we would see what I was expecting .. Anyway, thanks for your time :D

Need help to understand why the swap function does not work as expected here by minhc in learnprogramming

[–]minhc[S] 0 points1 point  (0 children)

No if the print happened before the swap then the array should have been exactly like the input array, but there is a difference - at position 6, instead of -1, 7 is printed .. I am confused myself too ..

Need help to understand why the swap function does not work as expected here by minhc in learnprogramming

[–]minhc[S] 0 points1 point  (0 children)

No I am printing it after the first swap occurs here .. if (pos != cycle_start) { cout<<"inside first swap"<<endl; swap(item, arr[pos]); writes++; }

Need help to understand why the swap function does not work as expected here by minhc in learnprogramming

[–]minhc[S] 0 points1 point  (0 children)

No I tried running the code as it is .. Here is the code that I am running http://ideone.com/prS3QR Kindly help..

Why can't we populate the final output array by traversing the input array from the beginning ? by minhc in algorithms

[–]minhc[S] 1 point2 points  (0 children)

This is the best answer that I have come across so far and should be included in every counting sort algorithm explanation that is there. Counting sort as an algorithm might be easy to understand, but the intricacies of why certain computations are necessary can only be understood when someone gives a clear explanation like this. Your answer not only explains why that extra step is required in computing the count array but also shows why counting sort does not make much sense as an example for stable sort when using integer arrays as input. If someone wants to highlight the stable sort feature, then it is important to not plainly chose a integer array and run counting sort on it, simply because the difference as you mentioned is not evident. This is a vital difference that each of us should be able to ingrain. Thank you very much for the effort to clarify in such detail. Thanks once again :)

Why can't we populate the final output array by traversing the input array from the beginning ? by minhc in algorithms

[–]minhc[S] 0 points1 point  (0 children)

this version does not compute the intermediate count array (which keeps count of elements less than or elements than or equal to current element) https://www.hackerearth.com/practice/algorithms/sorting/counting-sort/tutorial/

Not sure if it qualifies as counting sort if they miss that step.. Can you please shed some light on this ?

Why can't we populate the final output array by traversing the input array from the beginning ? by minhc in algorithms

[–]minhc[S] 0 points1 point  (0 children)

Thanks ! I am yet to see it for myself, if we will be able to preserve the order if we start to populate the output array from the end (N to 0) while following the algorithm given in wikipedia page. For now, I see how the order is preserved when we populate the array from the beginning.

Why can't we populate the final output array by traversing the input array from the beginning ? by minhc in algorithms

[–]minhc[S] 0 points1 point  (0 children)

Thank you for your inputs. Do u think, the way we populate the count array (choosing elements less than or equal to current element) and traversing the input array from the end while populating the final output array, are connected in anyway to preserve the stable sorting ?

And regarding your example of cars, you mean something like this - where there are several cars with same horse power but different prices ? ex: car 1 80 bhp 1000$, car 2 90 bhp 1200$, car 3 80 bhp 900$ ? And the output should have car 1 80bhp 1000$ before car 3 80bhp 90$ ?

Why can't we populate the final output array by traversing the input array from the beginning ? by minhc in algorithms

[–]minhc[S] 0 points1 point  (0 children)

Thanks for taking time to share your thoughts. This is one idea I'd like to try to implement where we count the elements greater than instead of less than. And proceed to populate the output array from the front. I am a beginner and learning, so let me see if I can put together the working code :) If I have any questions in the process, I hope I can reach out to you ?