Download code

Jump to: navigation, search

Back to Selection_sort_(C_Plus_Plus)

Download for Windows: zip

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

selection_sort.hpp

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Selection_sort_(C_Plus_Plus)?oldid=12755
14 */
15 
16 #ifndef SELECTION_SORT_HPP_
17 #define SELECTION_SORT_HPP_
18 
19 #include <algorithm>
20 
21 template < typename T, typename compare_less >
22 void selection_sort_array(T a[], int num_elements)
23 {
24     for(int sorted_size=0; sorted_size < num_elements; sorted_size++)
25     {
26         int min_element_index = sorted_size;
27         T min_element_value = a[sorted_size];
28         for(int i=sorted_size+1; i<num_elements; i++)
29         {
30             if (compare_less(a[i], min_element_value))
31             {
32                 min_element_index = i;
33                 min_element_value = a[i];
34             }
35         }
36         a[min_element_index] = a[sorted_size];
37         a[sorted_size] = min_element_value;
38     }
39 }
40 template < typename RandomIterator, typename compare_less >
41 void selection_sort(RandomIterator start, RandomIterator end)
42 {
43     int num_elements = end - start;
44     for(int sorted_size=0; sorted_size < num_elements; sorted_size++)
45     {
46         int min_element_index = sorted_size;
47         for(int i=sorted_size+1; i<num_elements; i++)
48         {
49             if (compare_less(*(start + i), *(start + min_element_index)))
50             {
51                 min_element_index = i;
52             }
53         }
54         std::swap(*(start + min_element_index) = *(start + sorted_size));
55     }
56 }
57 template < typename RandomIterator >
58 void selection_sort(RandomIterator start, RandomIterator end)
59 {
60     int num_elements = end - start;
61     for(int sorted_size=0; sorted_size < num_elements; sorted_size++)
62     {
63         int min_element_index = sorted_size;
64         for(int i=sorted_size+1; i<num_elements; i++)
65         {
66             if (*(start + i) < *(start + min_element_index))
67             {
68                 min_element_index = i;
69             }
70         }
71         std::swap(*(start + min_element_index), *(start + sorted_size));
72     }
73 }
74 
75 #endif


hijacker
hijacker
hijacker
hijacker

selection_sort_test.cpp

 1 /* The authors of this work have released all rights to it and placed it
 2 in the public domain under the Creative Commons CC0 1.0 waiver
 3 (http://creativecommons.org/publicdomain/zero/1.0/).
 4 
 5 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 
13 Retrieved from: http://en.literateprograms.org/Selection_sort_(C_Plus_Plus)?oldid=12755
14 */
15 
16 #include <iostream>
17 #include <vector>
18 #include <stdlib.h>   // for atoi(), rand()
19 #include "selection_sort.hpp"
20 
21 using std::cout;
22 using std::endl;
23 
24 int main(int argc, char* argv[])
25 {
26     int num_elements = atoi(argv[1]);
27     int* a = new int[num_elements];
28 
29     for (int i=0; i<num_elements; i++)
30     {
31         a[i] = rand();
32     }
33     selection_sort(a, a + num_elements);
34     for (int i=1; i<num_elements; i++)
35     {
36         if (a[i] < a[i-1])
37         {
38             cout << "ERROR" << endl;
39         }
40     }
41 
42     std::vector<int> v;
43     for (int i=0; i<num_elements; i++)
44     {
45         v.push_back(rand());
46     }
47     selection_sort(v.begin(), v.end());
48     for (int i=1; i<num_elements; i++)
49     {
50         if (v[i] < v[i-1])
51         {
52             cout << "ERROR" << endl;
53         }
54     }
55 
56     return 0;
57 }


hijacker
hijacker
hijacker
hijacker