| | |
counting comparisons when sorting
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2004
Posts: 3
Reputation:
Solved Threads: 0
i'm trying to keep track of how many comparisons and swaps my sorting algorithms are making. i know it's probably a variable referencing problem, but i can't seem to get it to work right. it's fine when i run only one of the sorts, but if i run both, moves comes out to 0. how can i fix this? here's a sample.
C++ Syntax (Toggle Plain Text)
InsertionSort(myarray1, size1); BubbleSort(myarray1, size1); void InsertionSort(Item a[], int Numitems) { int InsertItem, InnerCount, TempValue; int swaps = 0; int comparisons = 0; //move through list left to right for (InsertItem = 1; InsertItem < Numitems; InsertItem++) { //copy that value to a temp value TempValue = a[InsertItem]; //move from that item to the left for (InnerCount = InsertItem - 1; InnerCount >= 0 && TempValue < a[InnerCount]; InnerCount--) { comparisons += 1; //move values to right a[InnerCount+1] = a[InnerCount]; } //put insert value into correct spot a[InnerCount+1] = TempValue; swaps +=1; } for(int i=0; i<Numitems; i++) cout<<a[i]<<" " <<endl; cout<<"swaps = "<<swaps<<endl; cout<<"comparisons = "<<comparisons; } void BubbleSort(Item a[], int Numitems) { int NumPasses; int InnerCount; Item TempValue; int swaps = 0; int comparisons = 0; //start outer loop for (NumPasses=0; NumPasses < Numitems-1; NumPasses++) { //start inner loop for (InnerCount=0; InnerCount < Numitems-1; InnerCount++) { //compare adjacent values if (a[InnerCount] > a[InnerCount+1]) { //swap values TempValue = a[InnerCount]; a[InnerCount] = a[InnerCount+1]; a[InnerCount+1] = TempValue; swaps+=1; } comparisons += 1; } } for(int i=0; i<Numitems; i++) cout<<a[i]<<" " ; cout<<"swaps = "<<swaps<<endl; cout<<"comparisons = "<<comparisons<<endl; }
>it's fine when i run only one of the sorts, but if i run both, moves comes out to 0.
If you've sorted the list, just how many swaps do you think you'd need to sort the list?
If you've sorted the list, just how many swaps do you think you'd need to sort the list?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Aug 2004
Posts: 3
Reputation:
Solved Threads: 0
i'm not sure if i understand your question. of course after it's sorted there won't be any swaps needed. the part that i left out of the code is where i fill the array with random numbers (sorry). so, naturally, it would depend on the numbers & how mixed up in the array they are.
[edit] oh ok, i see what youre saying!!, since i've done the sort once, it's already sorted. duh! thanks.
[edit] oh ok, i see what youre saying!!, since i've done the sort once, it's already sorted. duh! thanks.
![]() |
Similar Threads
- Complexity of sorting techniques (C)
- Using database tools inside VB6 (Visual Basic 4 / 5 / 6)
- sorting problem (C++)
- Sorting (Java)
- Sorting (Java)
Other Threads in the C++ Forum
- Previous Thread: storing large numbers
- Next Thread: help me develop banking system
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






