Download code

From LiteratePrograms

Jump to: navigation, search

Back to Quicksort_(JavaScript)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

quicksort.js

 1 /* Copyright (c) 2008 the authors listed at the following URL, and/or
 2 the authors of referenced articles or incorporated external code:
 3 http://en.literateprograms.org/Quicksort_(JavaScript)?action=history&offset=20070102180347
 4 
 5 Permission is hereby granted, free of charge, to any person obtaining
 6 a copy of this software and associated documentation files (the
 7 "Software"), to deal in the Software without restriction, including
 8 without limitation the rights to use, copy, modify, merge, publish,
 9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
12 
13 The above copyright notice and this permission notice shall be
14 included in all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 
24 Retrieved from: http://en.literateprograms.org/Quicksort_(JavaScript)?oldid=8410
25 */
26 
27 Array.prototype.swap=function(a, b)
28 {
29 	var tmp=this[a];
30 	this[a]=this[b];
31 	this[b]=tmp;
32 }
33 
34 
35 function partition(array, begin, end, pivot)
36 {
37 	var piv=array[pivot];
38 	array.swap(pivot, end-1);
39 	var store=begin;
40 	var ix;
41 	for(ix=begin; ix<end-1; ++ix) {
42 		if(array[ix]<=piv) {
43 			array.swap(store, ix);
44 			++store;
45 		}
46 	}
47 	array.swap(end-1, store);
48 
49 	return store;
50 }
51 
52 
53 function qsort(array, begin, end)
54 {
55 	if(end-1>begin) {
56 		var pivot=begin+Math.floor(Math.random()*(end-begin));
57 
58 		pivot=partition(array, begin, end, pivot);
59 
60 		qsort(array, begin, pivot);
61 		qsort(array, pivot+1, end);
62 	}
63 }
64 
65 
66 function quick_sort(array)
67 {
68 	qsort(array, 0, array.length);
69 }
70 
71 
72 function dosort(form)
73 {
74 	var array=form.unsorted.value.split(/ +/);
75 
76 	quick_sort(array);
77 
78 	form.sorted.value=array.join(' ');
79 
80 
81 }
82 
83 


quicksort.html

 1 <html>
 2 <head>
 3  <title>Quicksort test</title>
 4   <script type="text/javascript" src="quicksort.js" >
 5   </script>
 6 </head>
 7 <body>
 8  <form id="qsform" action="" method="" >
 9   <input type="text" size="80" name="unsorted" 
10    value="Paris London Stockholm Berlin Oslo Rome Madrid Tallinn Amsterdam Dublin" /> <br />
11   <input type="button" name="sort" value="Sort" onclick="dosort(this.form)" /> <br />
12   <input type="text" size="80" name="sorted" value="" />
13  </form>
14 </body>
15 </html>
16 
17 


Views
Personal tools