VoyForums
[ Show ]
Support VoyForums
[ Shrink ]
VoyForums Announcement: Programming and providing support for this service has been a labor of love since 1997. We are one of the few services online who values our users' privacy, and have never sold your information. We have even fought hard to defend your privacy in legal cases; however, we've done it with almost no financial support -- paying out of pocket to continue providing the service. Due to the issues imposed on us by advertisers, we also stopped hosting most ads on the forums many years ago. We hope you appreciate our efforts.

Show your support by donating any amount. (Note: We are still technically a for-profit company, so your contribution is not tax-deductible.) PayPal Acct: Feedback:

Donate to VoyForums (PayPal):

Login ] [ Contact Forum Admin ] [ Main index ] [ Post a new message ] [ Search | Check update time | Archives: 1 ]
Subject: Re: Quicksort - tengdir listar


Author:
Hjįlmtżr
[ Next Thread | Previous Thread | Next Message | Previous Message ]
Date Posted: 00:31:54 04/29/04 Thu
In reply to: Balli 's message, "Re: Quicksort - tengdir listar" on 17:35:01 04/28/04 Wed

>OK, ég hef veriš aš spį mikiš ķ žetta. Hvaš er
>vitlaust viš žetta hér aš nešan. Eša mundi mašur
>kanski undir flestum kringumstęšum dęla öllu inn ķ
>fylki first og žašan aftur inn ķ tengda listan?
>
>kv, Balli
>

Nei ekki endilega. Ég er bśinn aš laga forritiš sem žś settir inn. Ég myndi kannski ekki skrifa žetta svona
sjįlfur frį grunni, en ég reyndi aš laga bara forritiš žitt og gera žaš lķkara upphaflega Quicksort. Helstu vandamįliš
hjį žér voru smįruglingur į bendunum (einhver stašar Tail ķ staš head, o.s.frv.).

Ég held reyndar aš žaš sé hęgt aš gera žetta įn žess aš nota nśmer į hnśtana (žaš sem žś kallar count), en žaš
gęti flękt forritiš eitthvaš.

Auk žess get ég ekki ķmyndaš mér aš žetta sé hrašvirkara en upphaflega Quicksort, en žetta hentar ķ einhverjum kringumstęšum(?).



#include
#include
#include
using namespace std;

class node {
int item;
int count;
public:
node* next;
node* prev;
node(int i, int c) {
item = i;
count = c;
next = 0;
prev = 0;
}
node(int x, int c, node* p) {
item = x;
count = c;
next = 0;
prev = p;
}
~node(){}
void setNextNode(node *n) {
next = n;
}
void setCount(int nr) {
count = nr;
}
int getCount() {
return count;
}
int getItem() {
return item;
}
void inCount() {
count++;
}
void deCount() {
count--;
}
bool operator== (node n) const
{
if (count == n.getCount())
return true;
else
return false;
}
bool operator< (node n) const
{
if (count < n.getCount())
return true;
else
return false;
}
};

template
void exch(Item &a, Item &b)
{
int t1 = a->getCount();
int t2 = b->getCount();
a->setCount(t2);
b->setCount(t1);
}

template
void quicksort(Item *first, Item *last)
{
if( first == 0 || last == 0 )
return;

if((first->getItem()) >= (last->getItem()))
return;

Item *Current = last; // Žetta er eins og r
Item *Tail = last->prev; // Žetta er eins og j
Item *head = first; // Žetta er eins og i

for (;;)
{
while (head->getCount() < Current->getCount()) {
head = head->next;
}

while ((Current->getCount()) < (Tail->getCount())) {
if (Tail == first)
break;
Tail = Tail->prev;
}

if((head->getItem()) >= (Tail->getItem()))
break;

exch(head, Tail);
}

exch(Current, head);

quicksort(first, head->prev);
quicksort(head->next, last);
}


int main()
{
int i;
node *nNode = new node(1, rand()%1000);
node *tNode;

for(i = 0;i < 19; i++) {
int iTala = rand()%1000;

tNode = nNode;

nNode = new node(i + 2, iTala, tNode);
tNode->setNextNode(nNode);

}

tNode = nNode;

for(i = 0;i < 19; i++) {
cout << nNode->getCount() << " nr. " << nNode->getItem() << endl;
nNode = nNode->prev;
}

cout << nNode->getCount() << " nr. " << nNode->getItem() << endl;
quicksort(nNode, tNode);

cout << "Eftir Quicksort" << endl;
for(i = 0;i < 20; i++) {
cout << nNode->getCount() << " nr. " << nNode->getItem() << endl;
nNode = nNode->next;
}

char stop;
cin >> stop;

return 0;
}


[ Next Thread | Previous Thread | Next Message | Previous Message ]

Replies:
Subject Author Date
Re: Quicksort - tengdir listarBalli15:42:56 04/30/04 Fri


Post a message:
This forum requires an account to post.
[ Create Account ]
[ Login ]
[ Contact Forum Admin ]


Forum timezone: GMT+0
VF Version: 3.00b, ConfDB:
Before posting please read our privacy policy.
VoyForums(tm) is a Free Service from Voyager Info-Systems.
Copyright © 1998-2019 Voyager Info-Systems. All Rights Reserved.