xEonBuRn Posted February 21, 2004 Share Posted February 21, 2004 What algorithm would I use to find the median and the mode in a list of integers by recursion (separate methods, mode, median)? I am using Java, not that it matters algorithm-wise. Link to comment https://www.neowin.net/forum/topic/143190-finding-medianmode-by-recursion/ Share on other sites More sharing options...
0 super_serge Posted February 21, 2004 Share Posted February 21, 2004 (edited) You would arrange the numbers in ascending or descending order using a bubble sort while incrementing a variable each time to show how many there are. Then after that you would just divide by half once and then exit prog (doesnt make much sense but there isnt really any other way to get out of the recursiveness from what i know. It would go something like this (for median): int sortmedian() { int temp; static int count = 0; ?//this variable never loses value if(arrayofints[count] > arrayofints[count + 1] { temp = arrayofints[count]; arrayofints[count] = arrayofints[count + 1]; arrayofints[++count] = temp; } ?//function SHOULD (not will) keep sorting array to make values incrementing sortmedian(); return count/2; } i havent worked with recusive functions almost ever so there are almost definitely errors in there, but if there are and you find / fix them, could u plz post the final vers. here, i wanna learn more. Oh and if anyone knows how to do the mode, plz post, cuz i figure it would be very manual but im sure there is some way to do it without making custom variables. Edited February 21, 2004 by super_serge Link to comment https://www.neowin.net/forum/topic/143190-finding-medianmode-by-recursion/#findComment-1760275 Share on other sites More sharing options...
0 super_serge Posted February 21, 2004 Share Posted February 21, 2004 sry, double post Link to comment https://www.neowin.net/forum/topic/143190-finding-medianmode-by-recursion/#findComment-1760298 Share on other sites More sharing options...
0 CdCViRus Posted February 21, 2004 Share Posted February 21, 2004 to find the mode, recursively, you need a recursive sort i suggest quicksort / mergesort to name just 2 - there are more recursive sorting algorithms do a google on quicksort to find the median, first sort the data using above techniques, and then take the middle datum Link to comment https://www.neowin.net/forum/topic/143190-finding-medianmode-by-recursion/#findComment-1761800 Share on other sites More sharing options...
Question
xEonBuRn
What algorithm would I use to find the median and the mode in a list of integers by recursion (separate methods, mode, median)?
I am using Java, not that it matters algorithm-wise.
Link to comment
https://www.neowin.net/forum/topic/143190-finding-medianmode-by-recursion/Share on other sites
3 answers to this question
Recommended Posts