sorting arrays (ascending order)

This is a discussion on sorting arrays (ascending order) within the C++ Programming forums, part of the General Programming Boards category; im trying to sort an array with an Array class. here's my code...i picked up some code from a website ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    sorting arrays (ascending order)

    im trying to sort an array with an Array class. here's my code...i picked up some code from a website and stuck it into my sort method...i don't know how else to do it though...note that the sort method has no parameters...that was strictly instructed by my teacher.

    Code:
    class Array
     {
      private:
      const static int declared_size = 100;
      arrElem array[declared_size];
      int numberUsed;
    
      public:
    
      void readFromFile(char fileName[])
      {
         ifstream infile(fileName);
    
         int filesize = 0;
         if (infile.fail())
        {
         cout << "Opening file " << fileName << " failed.  Goodbye." << endl;
         exit(1);
        }
    
        infile >> ws;
        while (infile >> array[filesize])
         {
          filesize++;
        if (filesize > 100)
          {
           cout << "There are more values in the file than there are positions"         
                << " in the array."
                << endl;
           exit(1);
          }
         }
      numberUsed = filesize;
      infile.close();
     }  
    
      int getSize(void)
       {
        return numberUsed;
       }
     
      void printArray(void)
       {
        int perLine = 5;
        for (int i = 0; i < numberUsed; i++)
        { 
         cout << array[i] << "      ";
         if ((i+1) % perLine==0)
          cout << endl;
        } 
       }
      
      void sort(void)
       {
        for (int i = 0; i < numberUsed; i++)
         {
          double x = array[i];
          int j;
        for ( j = 0; j < numberUsed; j++)
         {
          if (array[j] <= x)
              break;
          array[j+1] = array[j];
         }
         array[j+1] = x;
         }
       }
     
    };

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    what is ws?

    I recommend you do a search of the board using bubble sort as the search words. Bubble sort is probably the easiest sort to implement. There are different versions of bubble sort and there are different sorts other than bubble sort so you can choose.

    if(filesize > 100)

    means your program will crash, if you're lucky. If declared_size is 100 then the last valid value for filesize is 99, not 100.

  3. #3
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    I'm in the beginning stages of a similar program, so the coding is a tad flaky. See if you can use the sorting algorithm... a modified quick sort.

    http://ronin_akuma.tripod.com/index.html

    updated files link

    HTH
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Bubble sort is real easy (but very inefficient )

    There are several types of bubble sorts, the one i find easiest in pseudocode:

    Code:
    // sorts from lowest to highest
    for (x=0; x<arraySize-1; x++)
    {
        for (y=x+1; y<arraySize; y++)
        {
              if (array[x]>array[y])
                  swap 'em;
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help sorting arrays
    By Flyer in forum C++ Programming
    Replies: 4
    Last Post: 07-12-2003, 04:37 PM
  2. sorting arrays..
    By vege^ in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:38 PM
  3. Help with sorting arrays
    By Silence in forum C Programming
    Replies: 5
    Last Post: 05-17-2002, 10:05 AM
  4. Sorting Arrays
    By Jax in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 11:35 AM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21