Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

question about sorting

Hello everyone ... : )

i'm studying data structure and i'm trying to collect all the point in this course . what I have now is a question about sorting . I want to learn how can I do it and code it in the right way ..

The median of set with an old number of elements is the middle value if data items are arranged in order. An efficient algorithm to find the median that does not require first ordering the entire set can be obtained by modifying the quick sort algorithm. We use function split() to position a pivot element .If this pivot is positioned at location (n+1)/2,it is the median : otherwise, one of the two sublists produced by split() contains the median , and that sublist can be processed recursively. Write a function to fine the median of a list using this method.

this is the split()

:

template <typename ElementType>

void split (ElementType x[ ], int first, int last, int & pos)

{

ElementType pivot = x[first]; // pivot element

int left = first, // index for left search

right = last; // index for right search

while (left < right) { while (pivot < x[right]) // Search from right for element <= pivot

right--;
// Search from left for element > pivot

while (left < right && (x[left] < pivot || x[left] == pivot))
left++;
if (left < right) // If searches haven't met

swap (x[left, x[right]); // interchange elements

} // End of searches; place pivot in correct position

pos = right;

x[first] = x[pos];

x[pos] = pivot;
}
[1720 byte] By [Rooro] at [2007-11-11 10:03:16]
# 1 Re: question about sorting
It is worthwhile to put the algorithm into plain english - or at least pseudocode - to help you understand what your code needs to do.

Here, you calculate which element (number - the floor of n/2, where n is the number of elements in the collection) is the midpoint element. Then you set a pivot (any one of a few methods, even randomly) and then create two subsets - one for values less than, the other equal to or greater than, the value of the pivot. You now determine how many elements are in the less than subset. If that number of elements is smaller than your midpoint element number, then the midpoint element value is in the "greater than" subset, if the number of elements in the smaller is greater than the midpoint element number, then the midpoint element is in the smaller than pivot value subset. You then are looking for the floor of n/2 element in the second piece, and the floor of n/2 minus number of values in the lower set-th element of the larger set. Continue with the same process until you are to a particular size of subset where it is easy to quickly bubble sort or insert sort the elements.
nspils at 2007-11-11 20:59:34 >
# 2 Re: question about sorting
nspils as you said , i want at least pseudocode to understand ....

thanks ,and i will try to do it .
Rooro at 2007-11-11 21:00:29 >
# 3 Re: question about sorting
Take a look at these pages for more info ...

("Quickselect")
http://www.ics.uci.edu/%7Eeppstein/161/960125.html

(Deterministic quickselect)
http://www.ics.uci.edu/%7Eeppstein/161/960130.html
nspils at 2007-11-11 21:01:38 >
# 4 Re: question about sorting
Thanks nspils : )
Rooro at 2007-11-11 21:02:39 >
# 5 Re: question about sorting
Algorithms of note:

Insertion sort (N squared, but N for sorted lists, it is smartest N squared sorts)
Shell sort (N ^ 7/6 or faster, N for sorted lists, simple to code, best for smallish lists)*
Bucket sort: (N but only for integers in a smallish range)
Quicksort & intro sort(?) -- Lg(N) but quicksort can go to N squared at random, intro fixed this and makes it Lg(N) always.

Shellsort uses an array of constants that determine its runtime. Last I heard, the best sequences gave N^7/6 but the possibility lingers that better can be achived. Its an excellent piece of code to study!
jonnin at 2007-11-11 21:03:38 >
# 6 Re: question about sorting
Thanks jonnin

: )
Rooro at 2007-11-11 21:04:42 >
# 7 Re: question about sorting
visit the mentioned site. http://en.wikipedia.org/wiki/Sorting_algorithm
here u can find all the sorting alogrithm with code sinnept. i have refered so many items.

thanks
bidesh at 2007-11-11 21:05:35 >
# 8 Re: question about sorting
Thanks bidesh for the useful link

realy , I need it .
Rooro at 2007-11-11 21:06:34 >
# 9 Re: question about sorting
He was asking about SELECTION algorithms, not sorting.
nspils at 2007-11-11 21:07:42 >