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

11y 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.


What is the algorithm to input 3 numbers and output them in ascending order?

To sort three numbers in ascending order, you can use a simple comparison-based algorithm. First, compare the first two numbers and swap them if the first is greater than the second. Then, compare the second number with the third and swap if necessary. Finally, check the first number against the second again to ensure they are in order. This process will yield the numbers in ascending order.


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.

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


How do you arrange numbers in ascending order in qbasic programming?

To arrange numbers in ascending order in QBASIC, you can use a simple sorting algorithm like bubble sort. First, store the numbers in an array. Then, repeatedly compare adjacent elements and swap them if they are in the wrong order until the entire array is sorted. Here's a basic example: DIM numbers(5) AS INTEGER ' (Assume numbers are already populated) FOR i = 0 TO 4 FOR j = 0 TO 4 - i - 1 IF numbers(j) &gt; numbers(j + 1) THEN SWAP numbers(j), numbers(j + 1) END IF NEXT j NEXT i This will sort the array 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.


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 another name for ascending order of numbers?

largest to smallest


How do you sort three nondescending numbers?

In ascending order, perhaps!


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.