Re: Re: Bubble search ( descending)
"Roger L. McElfresh" <[email protected]>
| Newsgroups | gmane.comp.programming.cppug |
|---|---|
| Message-ID | <000601c3a4af$fdf04580$89d1bd42@CL3035518A> |
Here is a shell sort for char & int. Hope this will help ----- Original Message ----- From: robertnorwood2000 To: [email protected] Sent: Thursday, November 06, 2003 8:01 AM Subject: [cppug] Re: Bubble search ( descending) Check out: http://ptgtraining.com/CyberClassroom/0025o8/ch05/05_06/index.htm You wil have to register. It is a pretty decsent book. Includes the code I believe you are looking for. --- In [email protected], Marcel <marcelsol@y...> wrote: > /* If adjacent items are out of order ==> swap them */ > the code is well commented, I would give a try on this > line > > --- John Payette <petote_22960@y...> wrote: > > I am having trouble finding the code changes I need > > to make to create > > a sort list in descending order. Anyone out there > > with some advice? > > > > #include <iostream> > > using namespace std; > > > > /* > > Bubble Sort Technique > > > > */ > > > > > > #define swap(a,b) { int t; t=a; a=b; b=t;} > > > > int bubble(int array[], int end, int start) > > /*Input & Output: > > array[]: array to be sorted, and sorted output > > after sorting from > > start to end inclusive > > start: start index of items to be sorted > > end: end index of items to be sorted > > bubble: int Boolean flag for success/failure for > > function > > */ > > { > > int i, j; > > int flag = 1; > > /* Make (end - start + 1) passes through the > > array bubbling the > > lightest (smallest) item to the top */ > > > > for(i=start;i<end;i++) // i < end not <= end since > > the last item > > will be sorted with itself > > { > > /* From the first element to the end of the > > unsorted section */ > > for(j=end;j>i;j--) > > { > > /* If adjacent items are out of order ==> > > swap them */ > > if(array[j-1]>array[j]) > > swap(array[j-1],array[j]); // bubble the > > lighter item up > > } > > } > > > > return flag; > > } > > > > > > int main() > > { > > const int maxIndex=16; > > int myFlag=1; > > int i=0; > > int sortThis[] = {9, 1, 2, 3, 4, 5, 3, 2, 2, 2, 1, > > 7, 70, 60, 50 , 40 > > , 30}; > > > > if(myFlag=bubble(sortThis, 0, maxIndex)) > > for(i=0; i<= maxIndex ; i++) > > cout << sortThis[i] << " "; > > > > return myFlag; > > } > > > > > > > > ------------------------ Yahoo! Groups Sponsor > > > > To unsubscribe from this group, send an email to: > > [email protected] > > > > > > > > Your use of Yahoo! Groups is subject to > > http://docs.yahoo.com/info/terms/ > > > > > > > __________________________________ > Do you Yahoo!? > Exclusive Video Premiere - Britney Spears > http://launch.yahoo.com/promos/britneyspears/ Yahoo! Groups Sponsor ADVERTISEMENT To unsubscribe from this group, send an email to: [email protected] Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.
Sorts.cpp
(text/plain, 1.3 KB)
// Sorts.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
using namespace std;
void ShellSort(char v[], int cnt);
template <class X> void SortNums(X *items, int cnt);
int main(int argc, char* argv[])
{
char str[] = "abcdefghijklmnopqrstuvwxyz0123456789";
int nums[] = {0,1,2,3,4,5,6,7,8,9};
int len = strlen(str);
int i;
cout << "*** Reverse Sort Routines ***" << endl;
cout << "String:" << endl;
cout << "Unsorted: \"" << str << "\"" << endl;
ShellSort(str, len);
cout << "Sorted: \"" << str << "\"" << endl;
cout << "Interger:" << endl;
cout << "Unsorted: ";
for(i=0; i<10; i++)
cout << nums[i];
SortNums(nums, 10);
cout << endl;
cout << "Sorted: ";
for(i=0; i<10; i++)
cout << nums[i];
cout << endl;
return 0;
}
void ShellSort(char v[], int cnt)
{
int gap, i, j;
char temp[80];
for(gap = cnt / 2; gap > 0; gap /= 2)
for(i = gap; i < cnt; i++)
for(j = i - gap; j >= 0; j -= gap)
{
if(strcmp(&v[j], &v[j+gap]) >= 0)
break;
temp[0] = v[j];
v[j] = v[j+gap];
v[j+gap] = temp[0];
}
}
template <class X> void SortNums(X *items, int cnt)
{
register int a, b;
X t;
for(a=1; a<cnt; a++)
for(b=cnt-1; b>=a; b--)
if(items[b-1] < items[b])
{
t = items[b-1];
items[b-1] = items[b];
items[b] = t;
}
}