[SOLVED] What is this sorting algorithm? (or a new one?)

Hello everyone!
Just before starting, i apologize for my grammar mistakes.
I found a new sorting algorithm but i'm not sure if i really found it. There are too many sorting algorithms and mine is a really simple one; so, i belive that it can be found years ago.
I searched popular sorting algorithms, but none of the them is the answer.
Here is algorithm:
* Search the numbers between brackets
[24 12 12 55 64 18 32 31]
* Find smallest one
[24 12 12 55 64 18 32 31]
^S
* Swap the first item between brackets with smallest one
[12 12 24 55 64 18 32 31]
* Find largest one
[12 12 24 55 64 18 32 31]
^L
* Swap the last item between brackets with largest one
[12 12 24 55 31 18 32 64]
* Move brackets by one.
12[12 24 55 31 18 32]64
* Continue from step one until the array is sorted
/* rottsort
Copyright (c) 2013 Bora M. Alper
#include <stdio.h>
void print_array (const int *array, const int length);
int rottsort_swap (int *x, int *y);
void rottsort (int *array, const int length);
int rottsort_largest (const int *array, const int start, const int end);
int rottsort_smallest (const int *array, const int start, const int end);
void print_array (const int *array, const int length) {
int i;
for (i=0; i < length; ++i)
printf ("%d ", array[i]);
putchar ('\n');
int main (void) {
int array[] = {24, 12, 12, 55, 64, 18, 32, 31};
print_array(array, 8);
rottsort(array, 8);
print_array(array, 8);
return 0;
int rottsort_swap (int *x, int *y) {
const int temp = *x;
*x = *y;
*y = temp;
void rottsort (int *array, const int length) {
int i, largest_pos, smallest_pos;
for (i=0; i < length/2; ++i) {
largest_pos = rottsort_largest(array, i, length-1-i);
rottsort_swap(&(array[largest_pos]), &(array[length-1-i]));
smallest_pos = rottsort_smallest(array, i, length-1-i);
rottsort_swap(&(array[smallest_pos]), &(array[i]));
int rottsort_largest (const int *array, const int start, const int end) {
int i, largest_pos = start;
for (i=start; i <= end; ++i)
if (array[i] >= array[largest_pos])
largest_pos = i;
return largest_pos;
int rottsort_smallest (const int *array, const int start, const int end) {
int i, smallest_pos = start;
for (i=start; i <= end; ++i)
if (array[i] <= array[smallest_pos])
smallest_pos = i;
return smallest_pos;
P.S.: If this is a new sorting algorithm, i name it as "rottsort". :)
Last edited by boraalper4 (2013-08-11 19:08:17)

Trilby wrote:
Because you already have two variables for largets and smallest, there is no reason to loop through the whole list twice to get each.  Loop through the list (or list subset) once, and in each loop check if the current item is smaller than smallest_pos or larger than largest_pos.
This will increase efficiency by a factor of two.
As written I believe it'd be less efficient than even a simple bubble sort.  With the above revision it may be comparable to a bubble sort.
Thanks for quick answer and advice. :) I will try to do that. When i'm done, i will post the new code.
Code is tested on codepad. (I edited the code on my phone so, sorry for formatting)
/* rottsort
Copyright (c) 2013 Bora M. Alper
#include <stdio.h>
void print_array (const int *array, const int length);
int rottsort_swap (int *x, int *y);
void rottsort (int *array, const int length);
void rottsort_find (int *smallest_pos, int *largest_pos, const int *array, const int start, const int end);
void print_array (const int *array, const int length) {
int i;
for (i=0; i < length; ++i)
printf ("%d ", array[i]);
putchar ('\n');
int main (void) {
int array[] = {24, 12, 12, 55, 64, 18, 32, 31};
print_array(array, 8);
rottsort(array, 8);
print_array(array, 8);
return 0;
int rottsort_swap (int *x, int *y) {
const int temp = *x;
*x = *y;
*y = temp;
void rottsort (int *array, const int length) {
int i, largest_pos, smallest_pos;
for (i=0; i < length/2; ++i) {
rottsort_find (&smallest_pos, &largest_pos, array, i, length-1-i);
rottsort_swap(&(array[largest_pos]), &(array[length-1-i]));
if (smallest_pos == length-1-i)
smallest_pos = largest_pos;
rottsort_swap(&(array[smallest_pos]), &(array[i]));
void rottsort_find (int *smallest_pos, int *largest_pos, const int *array, const int start, const int end) {
int i;
*smallest_pos = start;
*largest_pos = start;
for (i=start; i <= end; ++i) {
if (array[i] >= array[*largest_pos])
*largest_pos = i;
if (array[i] <= array[*smallest_pos])
*smallest_pos = i;
Last edited by boraalper4 (2013-08-11 15:21:48)

Similar Messages

  • HT5625 I'm trying to create a new Apple ID but when I was putting on my information, there's a box asking for an ITunes gift code. What's this and how can I get one?

    I'm trying to create a new Apple ID but when I was putting on my information, there's a box asking for an ITunes gift code. What's this and how can I get one?

    You're not obligated to insert a gift code. But you can purchase gift codes on the Apple online store (gift cards or certificates) or Apple Retail store
    Gift codes are pre paid cards so you can use credit on the iTunes Store to purchase content
    To create apple ID without payment method:
    http://support.apple.com/kb/HT2534?viewlocale=pt_PT

  • Whenever I open a new Firefox Browser, the other Firefox Browsers that I have minimized to the bottom of the screen all restore, (reopen up) onto the screen with the new opened Firefox Browser, how do I stop this? Just want the new one to open on screen

    Whenever I open a new Firefox Browser, the other Firefox Browsers that I have minimized to the bottom of the screen all restore, (reopen up) onto the screen with the new opened Firefox Browser, how do I stop this? Just want the new one to open on screen and the existing stay minimized.
    == This happened ==
    Every time Firefox opened
    == I think when a I updated or downloaded a newer version of FireFox at Firefox's suggestion.

    Hmm that does sound a bit strange - I use a system quite similar to yours (XP, FF 3.6.3), and don't see this behaviour.
    I would recommend trying to start Firefox in Firefox [[Safe Mode]] (make sure you close '''all''' running Firefox instances first), and see if this problem persists.
    If not, it could be some add-on causing this behaviour - restart Firefox in Safe Mode, disable all add-ons, and enable them one by one to find the guilty one.
    If it still happens in Firefox Safe Mode, some other program (or something in Windows itself) most likely is the cause. We may be able to help with this too, so let us know what works and provide as much additional information as possible to let us help you solve this.
    Please let us know how above works out, and if we can be of further assistance. Detailed feedback is appreciated and helps us to help you better, plus enables us to better help other users with similar issues.

  • HT4539 I followed the instructions given to turn my automatic download icon in my iphone 3 and still goes back to off position. Any idea how to fix this issue without getting a new one?

    I followed the instructions given to turn my automatic download icon in my iphone 3 and still goes back to off position. Any idea how to fix this issue without getting a new one?

    Something may have gone amiss on your iPhone.
    To attempt to correct this follow these steps:
    Backup your iPhone using iTunes or iCloud.
    Use iTunes to completely restore your iPhone as Factory new.
    Monitor battery usage to see if it is normal. If not, you may have a hardware problem - if so, contact Apple support.
    Restore your iPhone from your backup above.
    Check battery usage to confirm that it is still normal.

  • [SOLVED] What is this isus folder?

    Hi.  A folder named isus just suddenly appeared in my home folder.  It contains one file called update.ini.
    What is this folder and where did it come from?  Maybe I put it there, but I don't remember.
    I tried googling it, but I just get a whole buncha stuff about ASUS.
    Last edited by Pacopag (2013-02-26 21:55:36)

    Wow.  I do indeed have Maple 11 on my system. So that must be it.  It must've been there for quite a while and I just never noticed it before.
    Thanks a bunch.

  • Would this sorting algorithm work?

    I'm writing a sorting algorithm for a class that implements the Comparable interface. The class (called Memo) has a field, 'entryDate' of type 'Date', which itself has int fields for day, month, and year. I want to have the class sorted by date, earliest to latest, and then (for entries on the same day) by 'priority' (a field of type int; can be 1, 2 or 3).
    The one I've written seems to work based on a few tests, but I just want to make sure it will work all the time:
    (btw, i have to use the 'getEntryDate()' method because entryDate is a private field in the Memo class' superclass.
    public int compareTo(Memo memo)
    int comparison = getEntryDate().getYear() - memo.getEntryDate().getYear();
    if(comparison != 0) {
    return comparison;
    comparison = getEntryDate().getMonth() - memo.getEntryDate().getMonth();
    if(comparison != 0) {
    return comparison;
    comparison = getEntryDate().getDay() - memo.getEntryDate().getDay();
    if(comparison != 0) {
    return comparison;
    return priority - memo.priority;
    }

    Generally when you simply subtract one int value from another in the compareTo() method, you'll have to take care that you don't run into an overflow situation (because comparing Integer.MIN_VALUE and (Integer.MIN_VALUE-1) would result in unpleasant surprises otherwise). But in your case you can probably safely assume that all your values are well within the safe range for int.
    Also: If you have the chance to modify the Date chance I'd simply make the Date implement Comparable<Date> and delegate that comparison to the Date class.
    Also: are two Memos at the same date with the same priority considered equal? Don't they have any other fields? It's important that objects that are not equals (i.e. a.equals(b) returns false) don't compare to be the same (i.e. a.compareTo(b) should only return 0 iff a.equals(b) returns true). Otherwise you might run into situations where objects are silently dropped because some Collection classes consider them to be equal (SortedSet implementations are tricky here).

  • Suggestions what to do with current box, getting new one

    Hey guys just curious if I could get some ideas. My current machine (p4 3.4ghz EE, 1.2gb ram, BFG 7600GS 512mb) will be free for any purpose once I get my new build this week. I have several sata drives, an extra monitor, kb, mouse, etc. to make it fully functional alongside the new one if need be. What could I do with it? I don't need to sell it and would like some use of it. Any ideas?? I was thinking of a server, but I have a domain with host and tons of storage that does that more than well enough right now. Maybe a game server? What would you guys do ??
    Any ideas are much appreciated, TIA everyone

    Thanks guys!
    Yes, I didn't think of these. Installing different distros would be great for me to learn and explore linux further
    genisis300 wrote:a testing box stick ssh on it and use it for breaking your system
    mail server /spam filter thing
    What do you mean stick ssh and break the system? I like the sound of that But exactly what you mean ??
    And I like the sound of mail server. Now I mainly use GMail for it and other 2 accounts, what would be benefit to mail server?

  • [SOLVED] Replacing an existing Arch drive with a new one

    I want to swap out my SATA drive with a new one, without reloading anything, nor borking anything..... The original drive is 1TB, and the new HDD is also 1TB, albeit from different manufacturers.
    I have WIn 7 in a 256Gb partition, and Arch in the extended partitions worth 750Gb. The drive is a 1 TB drive. (Hitachi)
    Arch is my primary OS where I do all my design/cad work and office related stuff. Windows is only used as a gaming platform.
    I have been given a NIB 1Tb Seagate Constellation enterprise class drive. It is significantly faster than my existing drive. I wish to switch the drives out.
    I am really wanting to avoid a complete reload of both OS's.
    My son tells me to use "dd" which I am familiar with, but warns that upon reboot Windows will likely have issues and be very slow.
    I have a heavy workload at the moment, and just don't have a day to spend reloading both OS's from scratch.
    Is there a better way to image a drive, partition scheme, boot sector, the whole smash (I have a 2TB drive to hold the image) .......R&R the old drive, install the new drive, then have the software then copy back over to the new drive the image with the partitioning scheme and boot sector (syslinux) without borking anything up in either OS?
    Assistance is greatly appreciated. I have not needed to do this before...and I'm nervous.
    Thank You!
    Sincerely and respectfully,
    Dave
    Last edited by dcbdbis (2013-03-02 04:48:08)

    FWIW, check if your new drive is one meant for the newer 4K sectors.  If so, you should only blindly dd it if the old drive was also set up this way.  If not, then you may need to align the copied data to the 4K sectors, or set up the partitions on the new drive appropriately.  (Western Digital, for example, has an align tool that buzzes through the drive edging everything over to align properly.  If you don't do this and your drive is copied out of alignment, then you can get some slowdown.)
    Quite possibly not an issue for you, and there's more to it than I've typed.  It's not hard to do right if needed.  GParted can partition on the correct boundaries, and clonezilla quite possibly is aware as well (I haven't looked). You just need to see if your drives are set up as advanced format (or whatever it's called) and copy accordingly if needed.
    I only mention this because there are currently mixed drive types out there and a surprising number of people don't know anything about this and might be taken by surprise.

  • SORT KEY- Create a new one

    I have to create 2 new account sort keys KNB1-ZUAWA.
    One would be for Customer Purchase Order # (VBKD-BSTKD) and another one would be fore the sales order # (VBAK-VBELN).
    Please give suggestions how to do the same.
    Thanks

    Do u mean key or index?U can create secondary index for the table specified below!
    Regards,
    Reema

  • In old firefox the zoom out stayed. Is there a way for this to happen on the new one too?

    When I play on some sites I like them to be zoomed out so I can see more at once. But every time I change the screen I'm using it zooms back to the original. Is there a way to save the setting so I don't have to scroll so much?

    You can use one of these extensions to set a default font size and page zoom on web pages:
    * Default FullZoom Level: https://addons.mozilla.org/firefox/addon/default-fullzoom-level/
    * NoSquint: https://addons.mozilla.org/firefox/addon/nosquint/
    Check that you aren't running Firefox in permanent [[Private Browsing]] mode

  • I lost my Photoshop ELements 10 installation cd 3, what can I do to get a new one?

    Please help me! I need it for my Mac.

    Hi Alina,
    Please follow the instructions below carefully to download Photoshop Elements 10 for MAC :
    1. Navigate to Photoshop Elements 11 downloads page : http://www.adobe.com/cfusion/tdrc/index.cfm?product=photoshop_elements&loc=us
    2. Sign in with your Adobe ID.
    3. Copy and paste the following link on the address bar and hit enter :
    Adobe Photoshop Elements 10 English Mac
    http://trials3.adobe.com/AdobeProducts/PSE/10/osx10/PhotoshopElements_10_WWEFDJ.dmg
    4. Save the file.
    5. Run the .dmg file.

  • HT204053 I am getting alot of spam in my ICloud email account.  How can I totally delete this account and start a new one

    I am getting alot of spam in my ICloud Email.   How can I totally delete my account and set up a totally new one?

    You might have some better luck if you call your country number from http://support.apple.com/kb/HE57 and ask to speak with the Account Security Team.

  • My charger has stopped working. What can I do besides buy a new one?

    I have had my MacBook Pro since June of 2011. A week ago, my charger stopped working. I don't want to have to buy a new one if I can do something to fix it.

    They are not repairable so you need to buy a new one. Between 4 and 5 years is a pretty good usage from a charger. Try to buy an Apple certified one as many here have had issues with cheap third party chargers.
    Pete

  • Sorting algorithm ?

    what is the sorting algorithm used in Collection.sort ?
    Its not clear what sorting algorithm is used in Collection.sort
    http://download.oracle.com/javase/1.4.2/docs/api/java/util/Collections.html
    Can anybody throw some light on this ?

    roy wrote:
    It works on binary search algorithm.No.
    Binary search requires that the list already be sorted. You don't use binary search to do the sorting.
    This algorithm has two forms. The first takes a List and an element to search for (the "search key"). This form assumes that the List is sorted in ascending order according to the natural ordering of its elements. The second form takes a Comparator in addition to the List and the search key.The second form also assumes the list is sorted--according to the rules of the comparator.

  • What is this brush tool?

    This might be very stupid, because it is, but one day I was trying the different brush tools in elements 9 and I drew this white light with blue coming off the edges. As if it were a light. This was a default tool (I chose the colours of course, ignore the black background) However now i've tried to find it but can't draw this anymore, it was a default tool and setting so I'd like to be able to create this sort of thing in just one click again, can anyone tell me what brush type (along with any settings for the brushes) would be used to make this. Thank you for your help!

    Then follow the steps that Terri_F posted (starting with a black background):
    Start with a soft-edged brush of fairly large size:  550px or more.
    Set the Mode to "Screen".
    Choose a foreground colour which will become the diffuse glow -- a blue, in this case.
    Left-click once to establish the outer extent of the diffuse glow.
    Without moving the brush, hit the "[" key a few times to reduce the size of the brush and left-click.
    Repeat step 5.
    Ken

Maybe you are looking for

  • How do I set up my hp laserjet printer through Cisco router?

    I'm using my HP Laserjet 4000TN printer for my entire network.  I need to be able to use it with all my computers.  So I usually plug it into the modem via Cat5 cord and it grabs an IP address which I add in System Preferences and I'm good to go.  Ho

  • Error message says cannot open because of missing ssl3.dll

    Computer was working fine yestrday, but when I booted up today an error messge said Firefox would not open because my computer was missing ssl3.dll. Where do I find, replace and install this .dll?

  • Outlook 2007 Shared calendars problem

    Hi, We are having a problem with shared calendars in outlook 2007. A colleague went on holiday for two weeks and whilst he was away, he was seeing people accepting new meeting times sent by the PAs but despite being an invitee he wasnt recieving invi

  • Photoshop Elements 12 Editor will not load

    all of a sudden elements 12 editor will not load.  It freezes when it says initializing panels. Any suggestions!!

  • The update "Java for Mac OS X 10.6 Update 12" can't be installed.

    When I try to update the Java in my mac os x 10.6.8 using the Software Update, I get the message: The update "Java for Mac OS X 10.6 Update 12" can't be installed. A network error has occurred. Check your Internet connection, and then try again. What