Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Sorting Arrays and Counting the Number of Swaps

  1. #1
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Sorting Arrays and Counting the Number of Swaps

    I need to modify modules used in the book that run a bubble sort, selection sort, and insertion sort on an integer array such that each module keeps a count of the number of swaps it makes.

    How do I code for this?


    Then we have to design an application that uses 3 identical arrays of at least 20 integers. That calls each module on a different array, and display the number swaps made by each algorithm.



    Beginner course, Using Programming Logic and Design by Tony Gaddis (3rd Ed.) - Chapter 9


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Saint-Petersburg, Russia
    Posts
    33
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Sorting Arrays and Counting the Number of Swaps

    Hi!

    This exercise supposes that it is you who should write the solution, not anyone else. So you'd better try.

    Have you already written BubbleSort yourself? I think after you do this, the rest will be obvious to you.

    You can see the short explanation of the simplified BubbleSort version here:
    Bubble Sort - CodeAbbey
    And by the way here is the task exactly about counting swaps, so you can check your answers online.

    If you think it is hard to catch the whole bubblesort algorithm at once in the mind, see the following page:
    Bubble in Array - CodeAbbey
    Here is the description of "one step" of the bubble sort. You only need to repeat such steps until you find that last step gives no swaps at all.

  3. #3
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Sorting Arrays and Counting the Number of Swaps

    I have the code for a bubble sort but I don't know how to code for it to count the number of swaps?

    If I can see an example of a program even similar to the 2nd question I would understand more. Our book is more like a short print out and there isn't a whole lot of explanation. I'm feeling lost.

  4. #4
    Member
    Join Date
    Oct 2013
    Location
    Saint-Petersburg, Russia
    Posts
    33
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Sorting Arrays and Counting the Number of Swaps

    I have the code for a bubble sort but I don't know how to code for it to count the number of swaps?
    Just close this code and try to rewrite it yourself, using your hands and your head.

    If I can see an example of a program even similar to the 2nd question I would understand more.
    You can not master programming by begging for code. You could not learn just looking at the code. You really need to write the code yourself.

    And believe, this task is not hard.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,812
    My Mood
    Mellow
    Thanks
    177
    Thanked 626 Times in 613 Posts

    Default Re: Sorting Arrays and Counting the Number of Swaps

    I think by "module" you mean "method" in Java. If not, please clarify. It's important to use the correct terms so that we understand the task and can help you appropriately.

    For counting the number of swaps, since swapping is an important part of most sort algorithms, you may have a separate method for swapping two elements in an array, something like the following signature that will swap the elements at index1 and index2 in the int[] array, 'myArray':

    public void swapElements( int[] myArray, int index1, int index2 )

    If that's the case, simply add a counter, say numberOfSwaps, to the swap method that increments each time a swap is made. Remember to check and then reset the swap counter between each sort algorithm.

  6. The Following User Says Thank You to GregBrannon For This Useful Post:

    AThompsonCO (November 3rd, 2013)

  7. #6
    Junior Member
    Join Date
    Nov 2013
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Sorting Arrays and Counting the Number of Swaps

    Thank you for the links they are definitely helpful. Trying to learn on my own and it's pretty hard to piece things together. I'm not a college student I'm an adult trying to teach myself. With absolutely no programming background. So what seems 'easy' for other programmers to just figure out is quite a challenge for me as this is an entirely new topic. Note taking an intro course.

    I get all kinds of attitude I'm surprised by on forums when I do ask a question as if people are too good to work with a beginner. People have up to 75 different learning styles and I am definitely visual and hands-on equally. I try to find some examples to lead me working through the book first then try the hands on.

    When it doesn't make sense to me I ask, it's the only way I'll learn. Because staring at it won't make me 'get it' as osmosis hasn't worked for me yet and at 35 I know what I need to learn something.

    --- Update ---

    I do mean method. The programming logic book is a broad generalization on all programs I am trying to apply this to learning Java.

    --- Update ---

    I'm not sure why you're being an ass. But I'm not begging for anything. I'm glad you don't think this is hard this is probably what you do for a living where I have zero background or experience in this and trying to learn something new.

    You win the prize for trying to make people feel like shit.


    public class SortingComp{

    public static void main(String Args[]){
    int num1[] = { 26,45,56,12,78,74,39,22,5,90,87,32,28,11,93,62,79, 53,22,51 };
    int num2[] = { 26,45,56,12,78,74,39,22,5,90,87,32,28,11,93,62,79, 53,22,51 };
    int num3[] = { 26,45,56,12,78,74,39,22,5,90,87,32,28,11,93,62,79, 53,22,51 };
    int bubsortcomp = bubble_srt(num1,num1.length);
    int selcomp = selection_srt(num2,num2.length);
    int inscomp = insertion_srt(num3,num3.length);
    System.out.println("Number of comparison in Bubble Sort " + bubsortcomp);
    System.out.println("Number of comparison in Selection Sort " + selcomp );
    System.out.println("Number of comparison in Insertion Sort " + inscomp );
    }



    public static int bubble_srt( int a[], int n ){
    int i, j,t=0;
    int numswaps =0;
    for(i = 0; i < n; i++){
    for(j = 1; j < (n-i); j++){
    if(a[j-1] > a[j]){
    numswaps++;
    t = a[j-1];
    a[j-1]=a[j];
    a[j]=t;
    }
    }
    }
    return numswaps;
    }
    public static int selection_srt(int array[], int n){
    int numswaps = 0;
    for(int x=0; x<n; x++){
    int index_of_min = x;
    for(int y=x; y<n; y++){
    if(array[index_of_min]<array[y])
    index_of_min = y;
    }
    int temp = array[x];
    array[x] = array[index_of_min];
    array[index_of_min] = temp;
    numswaps ++;
    }
    return numswaps;
    }

    public static int insertion_srt(int array[], int n){
    // no swaps in insertion sort shifting based on comparison
    int comp = 0;
    for (int i = 1; i < n; i++){
    int j = i;
    int B = array[i];
    while ((j > 0) && (array[j-1] > B)){
    comp++;
    array[j] = array[j-1];
    j--;
    }
    array[j] = B;
    }
    return comp;
    }
    }

  8. The Following User Says Thank You to AThompsonCO For This Useful Post:

    pdderek (April 13th, 2014)

  9. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,512
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Sorting Arrays and Counting the Number of Swaps

    Quote Originally Posted by AThompsonCO View Post
    I'm not sure why you're being an ass. But I'm not begging for anything. I'm glad you don't think this is hard this is probably what you do for a living where I have zero background or experience in this and trying to learn something new.
    I'm sick and tired of people asking for help and when the person replying gives them advice on how to improve their posts and their chances of getting help they respond by hurling abuse.

    Do you really think that people will be more willing to help you if you start calling them a$$holes?
    Improving the world one idiot at a time!

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,150
    Thanks
    48
    Thanked 2,158 Times in 2,131 Posts

    Default Re: Sorting Arrays and Counting the Number of Swaps

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,812
    My Mood
    Mellow
    Thanks
    177
    Thanked 626 Times in 613 Posts

    Default Re: Sorting Arrays and Counting the Number of Swaps

    And if you are going to hurl abuse at people, you should be sure to indicate at whom the abuse is thrown. I'll assume you were complaining at rodiongork, so I won't cross you off my Christmas list, but you move to the bottom. A better way to handle posts you don't find helpful is to ignore them. Assaults and abuse directed at any will alienate you with all. A bad reputation on social/professional forums like this one is hard to recover from, unfruitful (unless you count negative effects), and unnecessary.

    Good luck!

  12. #10
    Junior Member
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Sorting Arrays and Counting the Number of Swaps

    These are frustrated students you're interacting with. Give 'em a break. You're a professional who spends ALL DAY thinking about this after years of practicing it. Imagine working full-time and going to school full-time in pursuit of a Comp Sci degree purely online with ZERO understanding of programming until 2-months ago. That's me! I don't say this for pity, but just to seek some empathy from the pros.

    Sometimes we get stuck and have NO frame of reference to trouble-shoot from. We're looking for hints, for tips, for understanding, for an explanation I hadn't heard, an EXAMPLE (not the answer, an example), for a new brain to bounce ideas off of. We are not looking for links. We don't want to spend 3-more hours READING about a subject that we've already spent three hours reading about and another three hours coding a solution for that is still failing and finally we give up and ask for help from the inter-webs.

    I use these forums as a last resort. Perhaps you all are just sick of first-resort homework seekers. But try to imagine yourself alone, confused, pulling your hair out with no classroom to help you when you imagine the posters on these forums. Because most of us are not just seeking answers, we're seeking understanding.

    Sorry for the rant. It just bugs me to see how condescending the "professionals" are toward the students. I can't wait to work side-by-side with such nasty and inconsiderate people! I'll be promoted over them within weeks

  13. The Following User Says Thank You to pdderek For This Useful Post:

    newtolearningjava (April 13th, 2014)

  14. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,812
    My Mood
    Mellow
    Thanks
    177
    Thanked 626 Times in 613 Posts

    Default Re: Sorting Arrays and Counting the Number of Swaps

    Thread strayed off topic and should have died a quiet death.

    Thread closed.

Similar Threads

  1. Number Counting Array issues
    By compileDammit in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 25th, 2013, 08:12 PM
  2. Counting element(s) appearance(s) in arrays
    By alan55 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 20th, 2012, 01:03 AM
  3. Counting a total and printing value from 2 arrays
    By stewart86 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 2nd, 2012, 07:25 AM
  4. Counting Matches in Parallel Arrays
    By dx8292 in forum Object Oriented Programming
    Replies: 3
    Last Post: February 1st, 2012, 08:45 AM
  5. Help with Arrays - Counting elements
    By ShakeyJakey in forum Collections and Generics
    Replies: 7
    Last Post: August 8th, 2010, 04:09 PM

Tags for this Thread