c# - Quick Search out of bounds of array and confusing array behaviour -


i'm having multiple problems program (c#.net) , have no idea what's causing them.

the program intended sort list of names , birthdays (formatted first name,last name,dd/mm/yyyy) in ascending , descending order first name, last name, , birthday. have other functions have not yet been implemented.

the first problem in quiksortstr method. program crashes in first if block, stating j out of bounds of array. happens whether or not mode == "asc".

the second, , more confusing problem when values loaded text file, every odd-indexed value of first , last null, while each odd-indexed value of bday 1/1/0001.

i've included full program below reference, quicksort method , use of parallel arrays required. apologies lack of comments.

thanks in advance help. i'm stumped.

namespace names_arrays { public partial class frmnamarrays : form {     system.globalization.cultureinfo culture = new system.globalization.cultureinfo("en-ca");     string[] first;     string[] last;     datetime[] bday;     string order = "asc";     string format = "d/m/yyyy";      public frmnamarrays()     {         initializecomponent();     }     private void write()     {         string[] lines = new string[first.length];          (int = 0; < lines.length; i++)             lines[i] = first[i] + ',' + last[i] + ',' + bday[i].tostring(format);          txtbxnames.clear();         txtbxnames.lines = lines;     }      private void load()     {         string[] lines = txtbxnames.lines;          first = new string[lines.length];         last = new string[lines.length];         bday = new datetime[lines.length];          int = 0;         foreach (string line in lines)         {             string[] data = line.split(',');              //there aren't lines split string[] of length less three,             //but reason program kept believing there are.             //patched leak.              if (data.length == 3)             {                 first[i] = data[0];                 last[i] = data[1];                 bday[i] = convert.todatetime(data[2], culture);             }             i++;         }     }     public datetime[] quiksorttim(datetime[] primary, string mode, int left, int right)     {         if (primary.length > 1)         {             int = left, j = right;             datetime pivot = primary[left + (right - left) / 2];              while (i <= j)             {                 if (mode == "asc")                 {                     while (datetime.compare(primary[i], pivot) < 0)                         i++;                     while (datetime.compare(primary[j], pivot) > 0)                         j--;                 }                 else                 {                     while (datetime.compare(primary[i], pivot) > 0)                         i++;                     while (datetime.compare(primary[j], pivot) < 0)                         j--;                 }                 if (i <= j)                 {                     datetime holdoverb = primary[i];                     primary[i++] = primary[j];                     primary[j--] = holdoverb;                      string holdover = last[i - 1];                     last[i] = last[j + 1];                     last[j] = holdover;                      holdover = first[i - 1];                     first[i] = first[j + 1];                     first[j] = holdover;                  }             }             if (j > left)                 primary = quiksorttim(primary, mode, left, j);             if (i < right)                 primary = quiksorttim(primary, mode, i, right);         }         return primary;     }      public string[] quiksortstr(string[] primary, string type, string mode, int left, int right)     {         if (primary.length > 1)         {             int = left, j = right;             string pivot = primary[left + (right - left) / 2];              while (i <= j)             {                 if (mode == "asc")                 {                     while (string.compare(primary[i], pivot) < 0)                         i++;                     while (string.compare(primary[j], pivot) > 0)                         j--;                 }                 else                 {                     while (string.compare(primary[i], pivot) > 0)                         i++;                     while (string.compare(primary[j], pivot) < 0)                         j--;                 }                 if (i <= j)                 {                     string holdover = primary[i];                     primary[i] = primary[j];                     primary[j] = holdover;                     if (type == "first")                     {                         holdover = last[i];                         last[i] = last[j];                         last[j] = holdover;                     }                     else                     {                         holdover = first[i];                         first[i] = first[j];                         first[j] = holdover;                     }                     datetime holdoverbeta = bday[i];                     bday[i] = bday[j];                     bday[j] = holdoverbeta;                     i++;                     j++;                 }             }             if (j > left)                 primary = quiksortstr(primary, type, mode, left, j);             if (i < right)                 primary = quiksortstr(primary, type, mode, i, right);         }         return primary;     }      private void frmnamarrays_sizechanged(object sender, eventargs e)     {         txtbxnames.width = this.width - 40;         txtbxnames.height = this.height - 157;     }      private void btnsort_click(object sender, eventargs e)     {         load();          switch (cbobxcategory.text)         {             case ("first name"):                 first = quiksortstr(first, "first", order, 0, first.length - 1);                 break;             case ("last name"):                 last = quiksortstr(last, "last", order, 0, last.length - 1);                 break;             case ("birthday"):                 bday = quiksorttim(bday, order, 0, bday.length - 1);                 break;             default:                 break;         }          write();     }      private void cbobxorder_selectedindexchanged(object sender, eventargs e)     {         if (cbobxorder.text == "ascending")             order = "asc";         else             order = "desc";     }      private void displayfile(string name)     {         streamreader filedata = new streamreader(name);          txtbxnames.lines = filedata.readtoend().split('\n');      }      private void mnuopen_click(object sender, eventargs e)     {         openfiledialog open = new openfiledialog();         open.filter = "text files|*.txt";         open.title = "select text file...";          if (open.showdialog() == dialogresult.ok && open.filename != "")             displayfile(open.filename);     }      private void mnuexit_click(object sender, eventargs e)     {         this.close();     } } } 

you have change code below in quiksortstr method inside if (i <= j) loop

    datetime holdoverbeta = bday[i];     bday[i] = bday[j];     bday[j] = holdoverbeta;     i++;     j--;//was: j++; 

and fix issue.


Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -