Page 1 of 1

Bubble Sort

Posted: Mon Aug 15, 2005 8:16 pm
by bdixon
Hi All,

Does anyone have an example of a bubble sort in a before subroutine?

Brad

Re: Bubble Sort

Posted: Mon Aug 15, 2005 9:08 pm
by mchaves
bdixon wrote:Hi All,

Does anyone have an example of a bubble sort in a before subroutine?

Brad
Hi Brad,

Why do you need a bubble sort procedure? I mean, could you clarify your need a bit more?

Posted: Mon Aug 15, 2005 9:52 pm
by bdixon
I have an array in a before subroutine that needs to be sorted before i save it out to a file eg
Array<1> = 123
Array<2> = 99
Array<3> = 125

I want to sort the array in the following way:
Array<1> = 99
Array<2> = 123
Array<3> = 125

Posted: Tue Aug 16, 2005 12:44 am
by clarcombe
Hello,

You will have to write a before stage routine that works in the following way

0037 procedure BubbleSort( ListToSort : in out Lists) is
0038
0039 ListIsSorted : BOOLEAN := FALSE;
0040
0041 begin -- BubbleSort
0042 while not( ListIsSorted) loop
0043 ListIsSorted := TRUE;
0044 for ThisIndex in ListToSort'FIRST ..
0045 Indexes'PRED( ListToSort'LAST) loop
0046 if not SortRelation( ListToSort( ThisIndex),
0047 ListToSort( Indexes'SUCC( ThisIndex))) then
0048 Swap( ListToSort( ThisIndex),
0049 ListToSort( Indexes'SUCC( ThisIndex)) );
0050 ListIsSorted := FALSE;
0051 end if;
0052 end loop;
0053 end loop;
0054 end BubbleSort;

This is pseudocode not Datastage Basic

Good luck

Posted: Tue Aug 16, 2005 2:38 am
by ray.wurlod
How are the input data being delivered?

Posted: Tue Aug 16, 2005 7:41 am
by dzdiver
Does it need to be a bubble sort for some reason?

Windows has built in sort filter command.

If you had your array in a file (or put it in one) like infile.txt...

You could have a prejob something like: -
sort < infile.txt > sortedfile.txt

Then in your job, pick up the sequential file sortedfile.txt

sort also has ability to take some parameters.
Below is a copy from help file about sort command.
================================================
Sort
Reads input, sorts data, and writes the results to the screen, to a file, or to another device.

sort [/r] [/+n] [/m kilobytes] [/l locale] [/rec characters] [[drive1:][path1]filename1] [/t [drive2:][path2]] [/o [drive3:][path3]filename3]

[command |] sort [/r] [/+n] [/m kilobytes] [/l locale] [/rec characters] [[drive1:][path1]filename1] [/t [drive2:][path2]] [/o [drive3:][path3]filename3]

Parameters

/r

Reverses the sort order; that is, sorts from Z to A, and then from 9 to 0.

/+n

Specifies the character position number, n, at which sort begins each comparison. For example, /+3 indicates that each comparison should begin at the third character in each line. Lines with fewer than n characters collate before other lines. By default, comparisons start at the first character in each line.

/m kilobytes

Specifies the amount of main memory to use for the sort, in kilobytes (KB). The memory used is always a minimum of 160 KB. If the memory size is specified, the exact specified amount (but at least 160 KB) is used for the sort, regardless of how much main memory is available.

The default maximum memory size when no size is specified is 90 percent of available main memory if both the input and output are files, and 45 percent of main memory otherwise. The default setting usually gives the best performance.

/l locale

Overrides the sort order of characters defined by the system default locale; that is, the language and Country/Region selected when Windows 2000 was installed. Currently, the only alternative to the default locale is the "C" locale, which is faster than natural language sorting and sorts characters according to their binary encodings.

/rec characters

Specifies the maximum number of characters in a record, or a line of the input file (the default is 4,096, and the maximum is 65,535).

[drive1:][path1]filename1

Specifies the file to be sorted. If no file name is specified, the standard input is sorted. Specifying the input file is faster than redirecting the same file as standard input.

/t [drive2:][path2]

Specifies the path of the directory to hold the sort command's working storage, in case the data does not fit in main memory. The default is to use the system temporary directory.

/o [drive3:][path3]filename3

Specifies the file where the sorted input is to be stored. If not specified, the data is written to the standard output. Specifying the output file is faster than redirecting standard output to the same file.


hth,
B.