frankdavid Posted June 2, 2008 Share Posted June 2, 2008 hello, i'm experienced in PHP, but i'm having a problem. i have an array containing things. like: array('apple', 'chocolate', 'milk', 'hotdog') at on step i ask the user: what do you like more, .... or ..... . And with the least question i must show a list containing the things in the correct order accoding to the user's liking. i know that i need to use quicksort but in the php code i saw for quicksort algorithms the computer compares two numbers. what should i do? PHP CODE: <?php $_SESSION['array'] = array(8, 9, 1, 176, -45, 27, 99, 31, 15, 35, 88, 74, 0 ); $_SESSION['cur'] = 1; $_SESSION['stack'][1]['l'] = 0; $_SESSION['stack'][1]['r'] = count($_SESSION['array'])-1; while ($_SESSION['cur'] != 0 ) { $_SESSION['l'] = $_SESSION['stack'][$_SESSION['cur']]['l']; $_SESSION['r'] = $_SESSION['stack'][$_SESSION['cur']]['r']; $_SESSION['cur']--; while ($_SESSION['l'] < $_SESSION['r'] ) { $_SESSION['i'] = $_SESSION['l']; $_SESSION['j'] = $_SESSION['r']; $_SESSION['tmp'] = $_SESSION['array'][(int)( ($_SESSION['l']+$_SESSION['r'])/2 )]; // partion the array in two parts. // left from $_SESSION['tmp'] are with smaller values, // right from $_SESSION['tmp'] are with bigger ones while ($_SESSION['i'] <= $_SESSION['j'] ) { while ($_SESSION['array'][$_SESSION['i']] < $_SESSION['tmp'] ) { $_SESSION['i']++; } while ($_SESSION['tmp'] < $_SESSION['array'][$_SESSION['j']] ) { $_SESSION['j']--; } // swap elements from the two sides if ($_SESSION['i'] <= $_SESSION['j'] ) { $_SESSION['w'] = $_SESSION['array'][$_SESSION['i']]; $_SESSION['array'][$_SESSION['i']] = $_SESSION['array'][$_SESSION['j']]; $_SESSION['array'][$_SESSION['j']] = $_SESSION['w']; $_SESSION['i']++; $_SESSION['j']--; } } if ($_SESSION['i'] < $_SESSION['r'] ) { $_SESSION['cur']++; $_SESSION['stack'][$_SESSION['cur']]['l'] = $_SESSION['i']; $_SESSION['stack'][$_SESSION['cur']]['r'] = $_SESSION['r']; } $_SESSION['r'] = $_SESSION['j']; } } print_r($_SESSION['array']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/108439-help-quicksort/ Share on other sites More sharing options...
discomatt Posted June 2, 2008 Share Posted June 2, 2008 This is a VERY ROUGH example of the way I'd do it. <?php session_start(); # Check if this is first execution if ( !is_array( $_SESSION['foods'] ) ) $_SESSION['foods'] = array( 'apple', 'banana', 'pear', 'tangerine', 'melon', 'orange', 'pineapple' ); # Resort, if necessary $_SESSION['foods'] = resort( $_SESSION['foods'] ); # Display current favs display( $_SESSION['foods'] ); echo '<br /><br />'; # Ask question to narrow results further question( $_SESSION['foods'] ); function display( $arr ) { echo '<b>Order of like to dislike</b><br />'; echo implode( '<br />', array_reverse( $arr ) ); } function question ( $arr ) { $c = array_rand( $arr, 2 ); echo 'Which do you like better?<br />'; echo '<a href="?c=0">' . $arr[$c[0]] . '</a> or <a href="?c=1">' . $arr[$c[1]] . '</a>'; $_SESSION['c'] = $c; } function resort ( $arr ) { $x = $y = array(); if ( is_numeric( $_GET['c'] ) && is_array( $_SESSION['c'] ) ) { if ( $_GET['c'] == 0 && $_SESSION['c'][0] < $_SESSION['c'][1] ) { $replace = $_SESSION['c'][1]; $with = $_SESSION['c'][0]; } elseif ( $_GET['c'] == 1 && $_SESSION['c'][1] < $_SESSION['c'][0] ) { $replace = $_SESSION['c'][0]; $with = $_SESSION['c'][1]; } else return $arr; foreach( $arr as $key => $val ) { if ( $key == $with ) continue; elseif ( $key <= $replace ) $x[] = $val; else $y[] = $val; } return array_merge($x, array($arr[$with]), $y); } else return $arr; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/108439-help-quicksort/#findComment-555999 Share on other sites More sharing options...
frankdavid Posted June 5, 2008 Author Share Posted June 5, 2008 Thank you for replying but there is one problem with yours, that it asks the user until infinity. I would need one, that asks a few times and then write out the correct order. Quote Link to comment https://forums.phpfreaks.com/topic/108439-help-quicksort/#findComment-558206 Share on other sites More sharing options...
frankdavid Posted June 5, 2008 Author Share Posted June 5, 2008 help me, pls! Quote Link to comment https://forums.phpfreaks.com/topic/108439-help-quicksort/#findComment-558550 Share on other sites More sharing options...
discomatt Posted June 5, 2008 Share Posted June 5, 2008 Well, did you want random questions from a set like I did? Or set questions. It's really not hard to modify my script to do what you want... assuming you know PHP basics. Show me an attempted at getting it to do what you want it to... then I'll help further. I'm not here to code for you Quote Link to comment https://forums.phpfreaks.com/topic/108439-help-quicksort/#findComment-558674 Share on other sites More sharing options...
frankdavid Posted June 5, 2008 Author Share Posted June 5, 2008 I do not want you to code for me, but unfortunately I can't modify your code(I can but not know, how) cos it does a completely different algorithm, than the one does i would like. Yours selects two random elements and compares them. And i do not know how to ask only the needed questions. So pls help. (It's enough if you can tell me how can I modify the first code I sent in my first post or show a scheme that does I'd like.) THANK YOU : DAVID Quote Link to comment https://forums.phpfreaks.com/topic/108439-help-quicksort/#findComment-558691 Share on other sites More sharing options...
frankdavid Posted June 6, 2008 Author Share Posted June 6, 2008 no one has an idea? Quote Link to comment https://forums.phpfreaks.com/topic/108439-help-quicksort/#findComment-559007 Share on other sites More sharing options...
Daniel0 Posted June 6, 2008 Share Posted June 6, 2008 This is how I would do it: <?php $items = array('apple', 'chocolate', 'milk', 'hotdog'); $userItems = array(); echo str_repeat('=', 36) . PHP_EOL . 'Daniel\'s awesome preference selector' . PHP_EOL . str_repeat('=', 36) . PHP_EOL . PHP_EOL; while (count($items)) { echo 'Which of the following items do you like the most?' . PHP_EOL; foreach ($items as $num => $name) { $num++; echo " {$num}) {$name}" . PHP_EOL; } echo 'Enter choice: '; $num = trim(fgets(STDIN)) - 1; if (!key_exists($num, $items)) { echo 'Incorrect choice. Try again!' . PHP_EOL; continue; } echo "You choose: {$items[$num]}" . PHP_EOL; $userItems[] = $items[$num]; unset($items[$num]); sort($items); echo PHP_EOL; } echo PHP_EOL . 'You choose the items in the following order: ' . join(', ', $userItems); ?> Try to save it and run it like this from a console: php filename.php (needs the PHP executable in PATH) Your task: Change it from being a CLI script into being usable on a web server Quote Link to comment https://forums.phpfreaks.com/topic/108439-help-quicksort/#findComment-559014 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.