CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2022
    Posts
    14

    sorting values in a vector by the index of their numerical values

    [not resolved]

    your mission if you choose to accept it.....

    imagine you have a list of values {x, y, z, a, b, c}.
    you don't know what those values are going to be.
    you need to index those values according to their numerical value (smallest to largest or vice versa).
    so you can manipulate them and track which of the original values were used to make the new values (after they have been sorted)

    **Without using iota or sort**



    Code:
    #include <iostream>
    #include <vector>
    #include <random>
    #include <algorithm>
    
    void aftermath(std::vector <int>& vec_original_list)
    {
    	int var_count = vec_original_list.size();
    	int var_diff = 0;
    
    	std::random_device random_device_01;
    	std::mt19937 randgen_01(random_device_01());
    	std::uniform_real_distribution<> dis(0, 99);
    
    	std::vector<int> vec_math;
    	std::vector<int> vec_list_aftermath(var_count);
    
    	for (int var_create_vector_a = 0; var_create_vector_a < var_count; var_create_vector_a++)
    	{
    		var_diff = dis(randgen_01);
    		vec_math.push_back(var_diff);
    		vec_list_aftermath.push_back(0);
    	}
    
    	std::cout << "vec_original_list: ";
    	for (int var_aftermath = 0; var_aftermath < var_count; var_aftermath++)
    	{
    		std::cout << vec_original_list[var_aftermath] << " ";
    	}
    	std::cout << std::endl;
    
    	// manipulate values
    	std::cout << "vec_math: ";
    	for (int var_math = 0; var_math < var_count; var_math++)
    	{
    		if (vec_math[var_math] % 2 == 0)		vec_list_aftermath[var_math] = vec_math[var_math] + vec_original_list[var_math];
    		else									vec_list_aftermath[var_math] = vec_math[var_math] - vec_original_list[var_math];
    		std::cout << vec_math[var_math] << " ";
    	}
    	std::cout << std::endl;
    
    	std::cout << "vec_list_aftermath: ";
    	for (int var_aftermath = 0; var_aftermath < var_count; var_aftermath++)
    	{
    		std::cout << vec_list_aftermath[var_aftermath] << " ";
    	}
    	std::cout << std::endl << std::endl;
    
    }
    Code:
    void index_sort_function(std::vector <int>& vec_original_list)
    {
    	int var_count = vec_original_list.size();
    	std::vector<int> vec_original_list_sorted(var_count);
    	std::vector<int> vec_index_of_original_list(var_count);
    
    	for (int var_create_vector_a = 0; var_create_vector_a < var_count; var_create_vector_a++)
    	{
    		vec_original_list_sorted.push_back(0);
    		vec_index_of_original_list.push_back(0);
    	}
    
    	// copy and sort original values; largest to smallest
    	std::copy(std::begin(vec_original_list), std::end(vec_original_list), std::begin(vec_original_list_sorted));
    	std::sort(std::rbegin(vec_original_list_sorted), std::rend(vec_original_list_sorted));
    
    	// make vector of indexes in vec_index_of_original_list based on the size of contents within vec_number_list
    	// vector 'vec_index_of_original_list' provides the indexes of the values inside of vec_original_list; largest to smallest
    	std::cout << "vec_index_of_original_list: ";
    	for (int var_adjusted = 0; var_adjusted < var_count; var_adjusted++)
    	{
    		for (int var_sorted = 0; var_sorted < var_count; var_sorted++)
    		{
    			if (vec_original_list[var_sorted] == vec_original_list_sorted[var_adjusted])
    			{
    				vec_index_of_original_list[var_adjusted] = var_sorted;
    
    				std::cout << vec_index_of_original_list[var_adjusted] << " ";
    				break;
    			}
    		}
    	}
    	std::cout << "         Largest to smallest" << std::endl;
    }
    Code:
    void index_sort_function_dups(std::vector <int>& vec_original_list)
    {
    	bool var_dup_found = 0;
    	int var_count = vec_original_list.size();
    
    	std::vector<int> vec_original_list_sorted(var_count);
    	std::vector<int> vec_index_of_original_list(var_count);
    
    	for (int var_create_vector_a = 0; var_create_vector_a < var_count; var_create_vector_a++)
    	{
    		vec_original_list_sorted.push_back(0);
    		vec_index_of_original_list.push_back(0);
    	}
    
    	// copy and sort original values; largest to smallest
    	std::copy(std::begin(vec_original_list), std::end(vec_original_list), std::begin(vec_original_list_sorted));
    	std::sort(std::rbegin(vec_original_list_sorted), std::rend(vec_original_list_sorted));
    
    	// make vector of indexes in vec_ones_index_sorted based on the size of contents with vec_number_list
    	// and handle the duplicate values
    	std::cout << "vec_index_of_original_list: ";
    	for (int var_adjusted = 0; var_adjusted < var_count; var_adjusted++)
    	{
    		for (int var_sorted = 0; var_sorted < var_count; var_sorted++)
    		{
    			// reset
    			var_dup_found = 0;
    
    			// compare
    			if (vec_original_list[var_sorted] == vec_original_list_sorted[var_adjusted])
    			{
    				for (int var_check = 0; var_check < var_count; var_check++)
    				{
    					if (vec_index_of_original_list[var_check] == var_sorted)
    					{
    						//var_dup_found = 1;
    						break;
    					}
    				}
    
    				if (!var_dup_found)
    				{
    					vec_index_of_original_list[var_adjusted] = var_sorted;
    
    					// temporary outputs for verification
    					std::cout << vec_index_of_original_list[var_adjusted] << " ";
    					break;
    				}
    			}
    		}
    	}
    	std::cout << "         Largest to smallest" << std::endl;
    }

    Code:
    void main()
    {
    	std::cout << std::endl << std::endl << std::endl << std::endl;
    	std::vector<int> vec_original_list{ 1, 71, 25, 33, 18, 15, 6, 73, 68, 90 };
    	aftermath(vec_original_list);
    	//
    	index_sort_function(vec_original_list);
    	std::cout << std::endl << std::endl << std::endl << std::endl;
    	//
    	//
    	//
    	std::cout << "Notice the duplicate values in vec_original_list" << std::endl;
    	vec_original_list = { 1, 71, 25, 33, 1, 15, 6, 73, 68, 90 };
    	aftermath(vec_original_list);
    	//
    	index_sort_function(vec_original_list);
    	std::cout << std::endl;
    	std::cout << "two 0's and no '4'" << std::endl;
    	std::cout << "sorting function is unable to handle duplicate values :O" << std::endl;
    	std::cout << std::endl << std::endl << std::endl << std::endl;
    	//
    	//
    	//
    	std::cout << "Notice the duplicate values in vec_original_list" << std::endl;
    	aftermath(vec_original_list);
    	//
    	std::cout << "Still unable to handle the duplicate values" << std::endl;
    	index_sort_function_dups(vec_original_list);
    	std::cout << "how should should I rewrite this function?" << std::endl;
    }


    I got this answer from https://stackoverflow.com/...

    Code:
    std::vector<int> vec_original_list{ 1, 71, 15, 33, 18, 15, 6, 73, 68, 90 };
    std::vector<int> index(vec_original_list.size());
    
    std::iota(index.begin(), index.end(), 0);
    std::sort(index.begin(), index.end(), [&](int n1, int n2)
    		{ return vec_original_list[n1] < vec_original_list[n2]; });
    
    std::cout << "Original array\n";
    for (auto& i : vec_original_list)
    	std::cout << i << " ";
    
    std::cout << "\nSorted array\n";
    for (auto& i : index)
    	std::cout << vec_original_list[i] << " ";
    
    std::cout << "\nIndex array\n";
    for (auto& i : index)
        std:;cout << i << " ";
    ... but I can't use it because of how iota opperates




    I need a version of index_sort_function that can handle duplicate values.


    sample output:
    vec_original_list: 1 71 25 33 18 15 6 73 68 90
    vec_math: 50 0 93 58 22 65 30 13 40 32
    vec_list_aftermath: 51 71 68 91 40 50 36 -60 108 122

    vec_index_of_original_list: 9 7 1 8 3 2 4 5 6 0 Largest to smallest




    Notice the duplicate values in vec_original_list
    vec_original_list: 1 71 25 33 1 15 6 73 68 90
    vec_math: 91 74 9 77 4 25 48 66 84 93
    vec_list_aftermath: 90 145 -16 44 5 10 54 139 152 3

    vec_index_of_original_list: 9 7 1 8 3 2 5 6 0 0 Largest to smallest

    two 0's and no '4'
    sorting function is unable to handle duplicate values :O




    Notice the duplicate values in vec_original_list
    vec_original_list: 1 71 25 33 1 15 6 73 68 90
    vec_math: 5 85 91 78 82 55 20 59 29 10
    vec_list_aftermath: 4 14 66 111 83 40 26 -14 -39 100

    Still unable to handle the duplicate values
    vec_index_of_original_list: 9 7 1 8 3 2 5 6 0 0 Largest to smallest
    Last edited by maninthemiddle; December 15th, 2022 at 08:37 PM.

  2. #2
    Join Date
    Nov 2022
    Posts
    14

    Re: sorting values in a vector by the index of their numerical values

    updated original text
    Last edited by maninthemiddle; December 15th, 2022 at 08:36 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured