answersLogoWhite

0

It's hardly the most efficient way to sort integers. Integers are primitive data types, so it's actually quicker to sort the integers directly rather than point at them. Pointers just add an unnecessary level of indirection. Pointers should only be used for complex data types for which copying/moving would result in greater inefficiency than would otherwise be incurred through pointer manipulation.

#include<iostream>

#include<vector>

int main()

{

// instantiate a vector of unsorted integers

std::vector<int> v = {7,3,5,2,9,4,1,6,8};

// instantiate a vector of pointers to the integers in the vector

std::vector<int*> p;

for (size_t i=0; i<v.size(); ++i)

p.push_back (&v[i]);

// sort the pointers (uses insertion sort algorithm):

for (size_t i=1; i<p.size(); ++i)

{

int* x=p[i];

size_t gap = i;

size_t pre = i-1;

while (gap && *x<*p[pre])

{

p[gap--] = p[pre--];

}

p[gap]=x;

}

// print to prove the sorting worked:

std::cout << "Original order:\t\t\t";

for (size_t i=0; i<v.size(); ++i)

std::cout << v[i] << '\t';

std::cout<<std::endl;

std::cout << "Sorted order (by pointer):\t";

for (size_t i=0; i<p.size(); ++i)

std::cout << *p[i] << '\t';

std::cout<<std::endl;

}

User Avatar

Wiki User

10y ago

What else can I help you with?

Continue Learning about Engineering

What does descending mean in maths?

In Maths, we often talk about ascending and descending order. Ascending order is writing numbers from smallest to largest. Descending order is writing numbers from largest to smallest.


Ascending order program for java?

public class BubbleSortAscendingOrderDemo { public static void main(String a[]) { //Numbers which need to be sorted int numbers[] = {23,5,23,1,7,12,3,34,0}; //Displaying the numbers before sorting System.out.print("Before sorting, numbers are "); for(int i = 0; i &lt; numbers.length; i++) { System.out.print(numbers[i]+" "); } System.out.println(); //Sorting in ascending order using bubble sort bubbleSortInAscendingOrder(numbers); //Displaying the numbers after sorting System.out.print("Before sorting, numbers are "); for(int i = 0; i &lt; numbers.length; i++) { System.out.print(numbers[i]+" "); } }


How do you display 15 numbers in an ascending order using an array?

Sort the array then traverse the array, printing the element values as you go.


What does descending order mean?

Ascending means to increase or rise. You can ascend stairs, for example, by climbing them. a pattern of numbers can be ascending if each number increases, such as 1, 2, 3, 4, 5.Ascending is used to describe the return of Jesus from earth to heaven after his resurrection.ok thanks!!Going upwards. In different contexts it could mean increasing, getting bigger or climbing.


You can sort numeric fields in ascending order only?

No. They can be sorted either ways. Ascending or Descending.

Related Questions

What will be the program to arrange numbers stored in array in ascending order using pointers?

sorry


Draw a flowchart to arrange 3 numbers in ascending order?

draw a flow chart to arrange 3 numbers in ascending order


Vhdl code for assending order of numbers?

vhdl code for ascending order of numbers


What are Numbers listed least to greatest is in what order?

ascending


What is an order in which whole numbers are arranged from the least to the greatest?

ascending order


What does descending mean in maths?

In Maths, we often talk about ascending and descending order. Ascending order is writing numbers from smallest to largest. Descending order is writing numbers from largest to smallest.


What is ascending in Excel?

Ascending is an order in which things can be sorted. Ascending would be going from A to Z or lowest to highest numbers or earliest to latest dates.


What is another name for ascending order of numbers?

largest to smallest


How do you sort three nondescending numbers?

In ascending order, perhaps!


Which way is ascending and descending?

Ascending means increasing in value or moving higher, while descending means decreasing in value or moving lower. In a numeric sequence, ascending would go from lowest to highest, while descending would go from highest to lowest.


What is data organized in ascending or descending order?

The data that is being organized in ascending or descending order is the numbers or the alphabet. Ascending means is from lowest to highest and descending means from highest to lowest.


What is it called when you arrange numbers from greatest to least?

This is known as arranging the numbers in ascending order.