Please could you give me an example of sorting an array and how to call the function at the main

thanks a lot :o :rolleyes: :rolleyes:

Recommended Answers

All 14 Replies

how do you wish to sort it?

#include <algorithm>
#include <iostream>
#include <iterator>

using namespace std;

int main()
{
  int a[] = {5,7,4,6,3,8,0,1,9,2};

  sort ( a, a + 10 );
  copy ( a, a + 10, ostream_iterator<int> ( cout, " " ) );
}

;)

thanks alot for helping me but could you please give me an easier example (a more basic one) im new to programming

thank you :lol: :lol: :) :) :cry: :cry: :cry:

>please give me an easier example
That's as easy as it gets. If you want something more basic then you're looking at writing a sort function manually, and that's decidedly not basic compared to just calling std::sort.

Is this a homework assignment?

you didnt tell us how you want the array sorting! Perhaps highest value to lowest if its int?

thanks all for helping me

i want to sort the array from the lowest to the highest

its a part of my assignment

i've had a look in some books and i found a sorting function, the problem is that i don't know how to call it at the void main(). I think i need somehow to call the sorted array, any body can help pleazzzzzz!!

thanks again :cry: :cry: :cry:

Wouldn't you just define the function and then call it from within main()? I think you might want to review your textbook some more on functions. I'm not worth my salt as a programmer, and I could tell you this much.

Also, I'm sure Narue might mention this, but I know you're not supposed to use void main(). I'm not entirely sure why, and I don't want to guess, but I have an idea why. Care to elaborate further on this, Narue?

Also, I'm sure Narue might mention this, but I know you're not supposed to use void main(). I'm not entirely sure why, and I don't want to guess, but I have an idea why. Care to elaborate further on this, Narue?

I'm not Narue, but I try to be that good. I merely have this to offer.

An example in C, sorry but venerable old C was all I could find in a hurry!

// sort of an array of random integers using shell-sort 
// an optimized insertion sort developed by Donald Shell
// Pelles C

#include <stdio.h>
#include <stdlib.h>  // rand()

#define NUM_ITEMS 100

void shellSort(int numbers[], int array_size);

int numbers[NUM_ITEMS];

int main()
{
  int k;

  //fill array with random integers
  for (k = 0; k < NUM_ITEMS; k++)
    numbers[k] = rand() % NUM_ITEMS;

  //perform shell sort on array
  shellSort(numbers, NUM_ITEMS);

  // show the sorted numbers
  for (k = 0; k < NUM_ITEMS; k++)
    printf("%8d  ", numbers[k]);
  getchar();
  return 0;
}

// shell sort routine of n items in array[n]
void shellSort(int numbers[], int array_size)
{
  int i, j, increment, temp;

  increment = 3;
  while (increment > 0) 
  {
    for (i = 0; i < array_size; i++) 
    {
      j = i;
      temp = numbers[i];
      while ((j >= increment) && (numbers[j-increment] > temp)) 
      {
        numbers[j] = numbers[j - increment];
        j = j - increment;
      }
      numbers[j] = temp;
    }
    if (increment/2 != 0)
      increment = increment/2;
    else if (increment == 1)
      increment = 0;
    else
      increment = 1;
  }
}

thanks a lot vegaseat

Hello Nizar,

Now that you have some code to work with, it might help if you return to this post here and explain what the code does. In detail. To show that you have understanding of the material.

Christian

#include <algorithm>
#include <iostream>
#include <iterator>

using namespace std;

int main()
{
  int a[] = {5,7,4,6,3,8,0,1,9,2};

  sort ( a, a + 10 );
  copy ( a, a + 10, ostream_iterator<int> ( cout, " " ) );
}

;)

Narue, pardon my sciolism, but does ( a, a + 10 ) insinuate a vector?

>but does ( a, a + 10 ) insinuate a vector?
Not really. The standard algorithms take iterators, which only suggest a sequence. You give a beginning and an end, and the right thing just happens. You can think of the sequence as a vector, but that concept tends to break down when you consider iterators for non-linear containers such as std::set.

In the code I gave, sure, that analogy works because vectors are almost always implemented as an array. a is like saying v.begin() and a + 10 is like saying v.end(). However, it's not consistent with other potential uses of the iterator concept, so maintaining it would only serve to confuse.

Then again, I could be wildly misinterpreting your question. If that's the case, could you be more specific? :)

>but does ( a, a + 10 ) insinuate a vector?
Not really. The standard algorithms take iterators, which only suggest a sequence. You give a beginning and an end, and the right thing just happens. You can think of the sequence as a vector, but that concept tends to break down when you consider iterators for non-linear containers such as std::set.

In the code I gave, sure, that analogy works because vectors are almost always implemented as an array. a is like saying v.begin() and a + 10 is like saying v.end(). However, it's not consistent with other potential uses of the iterator concept, so maintaining it would only serve to confuse.

Then again, I could be wildly misinterpreting your question. If that's the case, could you be more specific? :)

Just my imagination, since you could use ( a, a + 10 ) to load the array to a vector and than sort. I imagined cutting out the middleman, in this case the vector. Comes from playing around with Python. I really liked your short C++ code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.