Int first = arr0; int second = arr0; int i; for(i=0;i. Your code requires approx. 2n comparisons. If you implemented the following method you can do it in approx n+log n comparisons. Organize a 'elimination style tournament' between the numbers.
For example you have 4 numbers, Greatest / W1 W2 / / X X X X Where the X's are your numbers, then you require n-1 comparisons to find the greatest. Then the second greatest could have only been beaten by the greatest number. So it is one of the log n numbers that greatest has beaten. Therefore you need log n comparisons to find second greatest, overall, approx n+log n instead of approx 2n.
The problem with your code is a logic problem (which is what most coding is about). If the largest number is first then it gets the second largest number wrong. Well, look at your logic for deciding on the second largest number. You first set it to be equal to the first element in the array and then you go through the array and change the index if the element is greater than the current second largest number (which will never be true because we already set it to be the largest number!). To solve it you can special case this: check if the largest number was the first and if so then set it to the second element (and then special case the issue of someone asking to find the highest two elements in a one element array, without reading past the end of an array.) I think the method given in chqrlie's answer to do this all in one pass is best.
GB MP3 Player. Com - A great place to buy computers, computer parts, electronics, software, accessories, and DVDs online. View and Download DANE-ELEC MINIPLAYER manual. Firmware and software updates are also available as well as the most. Video Converter for MEIZU MiniPlayer 3. Meizu mini player manual.
And logical too: write a program to find the largest number. Second largest number, well that's just the one which was previously the largest! If you ever need to find the largest or smallest element in an array, try with bubble sort. Bubble Sort works on simple concept of shifting the biggest element at the end in every pass it does (in case of increasing order). Since you need the first and second largest element in an array, 2 passes of bubble sort will do the trick.
The last element will be the largest and the second last element will be second largest. I'm providing you with the link that'll help you understand the bubble sort concept.
Hope it helps!!!
List Of Largest Numbers
Int first = arr0; int second = arr0; int i; for(i=0;i. Your code requires approx. 2n comparisons.
If you implemented the following method you can do it in approx n+log n comparisons. Organize a 'elimination style tournament' between the numbers. For example you have 4 numbers, Greatest / W1 W2 / / X X X X Where the X's are your numbers, then you require n-1 comparisons to find the greatest. Then the second greatest could have only been beaten by the greatest number. So it is one of the log n numbers that greatest has beaten. Therefore you need log n comparisons to find second greatest, overall, approx n+log n instead of approx 2n.
C program to find the largest and second largest numbers present in an array along with their location index on the array. This program first find the largest number by going through all the numbers in the array. It stores the index of largest number on the variable pos1 and largest number is largest variable. A second for loop is applied which also check for the largest number.
When it hits on the largest found by first loop, it is bypassed using a continue statement and hence will get the second largest number.
Int first = arr0; int second = arr0; int i; for(i=0;i. Your code requires approx.
2n comparisons. If you implemented the following method you can do it in approx n+log n comparisons.
Organize a 'elimination style tournament' between the numbers. For example you have 4 numbers, Greatest / W1 W2 / / X X X X Where the X's are your numbers, then you require n-1 comparisons to find the greatest. Then the second greatest could have only been beaten by the greatest number. So it is one of the log n numbers that greatest has beaten. Therefore you need log n comparisons to find second greatest, overall, approx n+log n instead of approx 2n.