Results 1 to 3 of 3

Thread: Help to set up sorting algorithm

  1. #1
    Join Date
    May 2012
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Help to set up sorting algorithm

    Hi all. I'm working on car simulator. The core is the class car who implements the physics of veicles. This class have a public function that return Qtime, the minimum time to go through a part of a track.
    One of the outputs of my application would be a table in a sqlite db like this:
    Lap | iDCar | LapTime | TotalTime | Position
    For this i have implemented a code like this (i'm not posing the original code couse a lot of function's names are not in english)
    Qt Code:
    1. QString query = " Insert into tablename ";
    2. QVector<Part> track;
    3. QVector<Car> cars;
    4. //fill vectots
    5. QMap<QString, QTime> total_time;
    6. QTime lapTime, partTime;
    7. for(int lap=1; lap<=nLap; lap++)
    8. {
    9. for(int id=0; id<cars.size(); id++)
    10. {
    11. lapTime = QTime(0,0,0,0);
    12. for(int part=0; part<track.size(); part++)
    13. {
    14. partTime = cars[id].getTime(track.at(part));
    15. lapTime = lapTime.addMsec(partTime.msecTo(QTime(0,0))); //really, qAbs() of partTime.msecTo()
    16. total_time[cars.at(id).pilotId()] = total_time[cars.at(id).pilotId()].addMsec(lapTime.msecTo(QTime(0,0)));
    17. s.append( // built a "select - union all" query.
    18. }
    19. }
    20. }
    21. qsqlQuery q;
    22. q.exec(query);
    To copy to clipboard, switch view to plain text mode 

    Now the problem is that i can't figure out how to get the position of the car at that determinated lap. I know that i have to do something with the QMap total_time that have unique car id as keys, and total time as values, but i don't know how to sort QMap values witout losing the corrispondence with keys.
    I hope someone could help me, and i apologize for my English.
    Last edited by Quasar; 24th May 2012 at 18:17. Reason: reformatted to look better

  2. #2
    Join Date
    May 2012
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Help to set up sorting algorithm

    Solved Creating a custom Class based upon QList.
    It works very well, it use a QList<QPair<QString, QTime>> as member and like a QMap have unique keys as QStrings.
    I'm posting the header of that class, so if anyone has troubles like mine, can implement a custom class like this, for exemple based on different QPair.

    PairTimeList.h
    Qt Code:
    1. #ifndef PAIRTIMELIST_H
    2. #define PAIRTIMELIST_H
    3. #include <QList>
    4. #include <QString>
    5. #include <QStringList>
    6. #include <QTime>
    7. #include <QPair>
    8.  
    9. class PairTimeList
    10. {
    11. public:
    12. PairTimeList();
    13. PairTimeList( QList< QPair<QString, QTime> > list);
    14.  
    15. bool contains(const QString key) const;
    16. bool contains(const QPair<QString, QTime> pair) const;
    17. bool insert(const QString str, const QTime t);
    18. bool insert(const QPair<QString, QTime> pair);
    19. bool erase(const QString key);
    20. bool erase(const QPair<QString, QTime> pair);
    21. bool erase(int position);
    22. void clear();
    23. void removeFirst();
    24. void removeLast();
    25. bool replace(int position, const QPair<QString, QTime> newpair);
    26. bool replace(const QString key, const QTime newTime);
    27. int position(const QString key) const; //return -1 if key doesn't exists
    28. QPair<QString, QTime> data(int position) const;
    29. const QPair<QString, QTime> at(int position) const;
    30. QTime time(const QString key) const;
    31. QList<QTime> times() const;
    32. QStringList keys();
    33. QStringList keys(const QTime value); //return all keys(QString) with QTime=value
    34. QStringList keysBetween(const QTime fromTime, const QTime toTime);
    35. const QList< QPair<QString, QTime> > sort_byTime() ; //BubbleSort
    36. const QList< QPair<QString, QTime> > sort_byStrings(); //BubbleSort
    37. const QList< QPair<QString, QTime> > getList() const ;
    38. QString print();
    39. int size() const;
    40. void setTime(int position, QTime t);
    41. void setTime(int position, int h, int min, int sec=0, int msec=0);
    42. void setTime(const QString key, QTime t);
    43. void setTime(const QString key, int h, int min, int sec=0, int msec=0);
    44. void addSec(int position, int sectoAdd);
    45. void addSec(const QString key, int sectoAdd);
    46. void addMsec(int position, int msecToAdd);
    47. void addMsec(const QString key, int msecToAdd);
    48. QTime deltaTime(int from1, int to2);
    49. int msecBetween(int position1, int position2);
    50. int msecBetween(const QString key1, QString key2);
    51. int secBetween(int position1, int position2);
    52. int secBetween(const QString key1, QString key2);
    53. private:
    54. QList< QPair<QString, QTime> > mylist;
    55. };
    56.  
    57. #endif // PAIRTIMELIST_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by Quasar; 25th May 2012 at 14:16. Reason: spelling corrections

  3. #3
    Join Date
    May 2012
    Location
    Ahmedabad, Gujrat, India
    Posts
    3

    Default Re: Help to set up sorting algorithm

    Bucket sort, or bin sort, is a sorting algorithm that works by partitioning an array into a number of buckets. Each bucket is then sorted individually, either using a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a distribution sort, and is a cousin of radix sort in the most to least significant digit flavour. Bucket sort is a generalization of pigeonhole sort. Since bucket sort is not a comparison sort, the Ω(n log n) lower bound is inapplicable. The computational complexity estimates involve the number of buckets.

    function bucketSort(array, n) is
    buckets ← new array of n empty lists
    for i = 0 to (length(array)-1) do
    insert array[i] into buckets[msbits(array[i], k)]
    for i = 0 to n - 1 do
    nextSort(buckets[i])
    return the concatenation of buckets[0], ..., buckets[n-1]
    Here array is the array to be sorted and n is the number of buckets to use. The function msbits(x,k) returns the k most significant bits of x (floor(x/2^(size(x)-k))); different functions can be used to translate the range of elements in array to n buckets, such as translating the letters A–Z to 0–25 or returning the first character (0–255) for sorting strings. The function nextSort is a sorting function; using bucketSort itself as nextSort produces a relative of radix sort; in particular, the case n = 2 corresponds to quicksort

Similar Threads

  1. No Response when Algorithm runs...
    By revellix in forum Qt Programming
    Replies: 2
    Last Post: 13th August 2011, 17:24
  2. qt algorithm speed up problem
    By leonardhead in forum Newbie
    Replies: 3
    Last Post: 27th August 2010, 01:03
  3. qtthread to speed up the algorithm
    By leonardhead in forum Qt Programming
    Replies: 1
    Last Post: 20th August 2010, 05:09
  4. Sutherland-Hodgman algorithm of clipping
    By YaK in forum General Programming
    Replies: 0
    Last Post: 29th March 2009, 10:54
  5. Dijkstra's Algorithm
    By therealjag in forum Qt Programming
    Replies: 2
    Last Post: 6th March 2006, 10:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.