We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Tutorials
  3. 30 Days of Code
  4. Day 20: Sorting
  5. Discussions

Day 20: Sorting

Problem
Submissions
Leaderboard
Discussions
Editorial
Tutorial

Sort 537 Discussions, By:

votes

Please Login in order to post a comment

  • jmjiru
    6 years ago+ 5 comments

    All I did for my Cpp code was copy the example code in the problem and paste it into the sollution and write out the info required. Definitely not a challenge.

    57|
    Permalink
    View more Comments..
  • michaelhazani
    5 years ago+ 2 comments

    We need to talk about the fact that often the tutorials in 30 days of code have nothing to do with the exercise. These things could be structured MUCH more efficiently.

    40|
    Permalink
  • vmunozisler
    6 years ago+ 10 comments

    My solution in JAVA:

    int totalSwaps=0;
    for (int i = n-1; i >0; i--) {
        int numberOfSwaps = 0;
        for (int j = 0; j <i; j++) {           
            if(a[j]>a[j+1]){
                int temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
                numberOfSwaps++;
                totalSwaps++;
            }
        }
        if (numberOfSwaps == 0) {
            break;
        }
    }   
    System.out.println("Array is sorted in "+totalSwaps+" swaps.");
    System.out.println("First Element: "+a[0]);
    System.out.println("Last Element: "+a[n-1]);
    
    18|
    Permalink
    View more Comments..
  • fyodor_kutsepin
    5 years ago+ 10 comments

    Python way

    def bubble(arr):
        count_swap = 0
        for num in range(len(arr)-1, 0, -1):
            for i in range(num):
                if arr[i] > arr[i+1]:
                    arr[i], arr[i+1] = arr[i+1], arr[i]
                    count_swap +=1
        return count_swap
    
    print "Array is sorted in {} swaps.".format(bubble(a))
    print "First Element: {}".format(a[0])
    print "Last Element: {}".format(a[-1])
    
    15|
    Permalink
    View more Comments..
  • neelamgupta88
    6 years ago+ 1 comment

    Hi, I think the Bubble sort Algorithm specified, has a little drawback. The inner loop should not be running to complete length it should be (length-1 - outer loop index) and outer loop also should be running to length-1. i.e. pseudo - algorithm should be this:

        for (int i = 0; i < n-1; i++) {
          int numberOfSwaps = 0;
    
          for (int j = 0; *j < n -i - 1 *; j++) {
             if (a[j] > a[j + 1]) {
               swap(a[j], a[j + 1]);
               numberOfSwaps++;
             }
           }
    
           if (numberOfSwaps == 0) {
             break;
           }
       }
    
    15|
    Permalink
Load more conversations

Need Help?


View tutorial
View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature