Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8692235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:16:43+00:00 2026-06-13T00:16:43+00:00

code should do this: a) Given an unsorted array of integers, your task is

  • 0

code should do this: a) Given an unsorted array of integers, your task is to sort the array by applying the following algorithm
(Assume that the input doesn’t contain duplicates ):
Execute the following steps starting from the first element in the array:
– Count the number of smaller elements to find the correct position i.
– If the element is in its correct position, move to the succeeding element.
– Otherwise, swap the current element with the one found in position i.
– Repeat the previous steps till you reach the last element.

Example:
5 7 3 6 9
check a[0], there is one element smaller that it, so it shoould be swapped with the element at
position1.
7 5 3 6 9
check the new element a[0]. it should move to position 3.
6 5 3 7 9
check the new element a[0]. it should move to position 2.
3 5 6 7 9
check the new element a[0]. it is in its correct position, so we move to the succeeding element a[1].

public class  Assignment1_T11_25_2729_Sara_Aly {
private int[] a;
private int max;
private int n;
int position=0;
public  Assignment1_T11_25_2729_Sara_Aly (int max){
    a= new int[max];
}
public void insert(int x){
    a[n]=x;
    n++;
        }
public void sort(){
    int out=0, smaller=0;
    while(out<n){
        for(int in=out+1;in<n;n++){
            if(a[in]<a[out])
                smaller++;
        }
        if (smaller==0){
            out++;
        }
        else {
            swap(a[out], a[smaller]);
        }
    }
    }
private void swap (int one, int two){
    int temp=a[one];
    a[one]=a[two];
    a[two]=temp;
}
    public void display(){
        for (int i=0;i<n;i++){
            System.out.print(a[i]+ " ");
        }
        System.out.println("");
    }
public static void main(String[]args){
    int maxsize=5;
    Assignment1_T11_25_2729_Sara_Aly trial;
    trial= new  Assignment1_T11_25_2729_Sara_Aly(maxsize);
    trial.insert(5);
    trial.insert(7);
    trial.insert(3);
    trial.insert(6);
    trial.insert(9);
    trial.display();
    trial.sort();
    trial.display();
}
}

    Tried a few algorithims to get it to work but for some reason it won't sort any suggestions??

also tried this the sorting method but no luck.

public void sort(){
boolean finished = false;
int position =0;
    while (position<max){
        if (finished==true){
        position++;
        finished =false;
        }
        else {
            int smaller=0;
            for (int j = position+1; j<max; j++){
            int temp=a[position];
                if (a[j] <a[position]){
                    smaller++;
                }
            }
            if (smaller==0){
                finished= true;
            }
            else {
                int temp= a[smaller];
                a[smaller]=a[position];
                a[position]=temp;
                }
                }
            }
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:16:43+00:00Added an answer on June 13, 2026 at 12:16 am

    Though you have not described exactly what the problem is, I would assume that, in your first code, the for-loop inside the while loop of your sort method is giving you a problem: –

        for(int in = out+1; in < n; n++) {
            if(a[in] < a[out])
                smaller++;
        }
    

    Here, you are incrementing n++ rather than in++. Check onto that. Change it to in++. You might be getting into infinite loop because of that.

    Also, there is a problem in your swap method. You have invoked your swap method with actual array element, but you are considering them as indices in the method.

    swap(a[out], a[smaller]);  // Called with element `a[out]`
    
    private void swap (int one, int two) {
    
        int temp=a[one];    // This is equivalent to a[a[out]]
        a[one]=a[two];
        a[two]=temp;
    }
    

    You can just pass index to your method: –

    So, invoke your swap method like : – swap(out, smaller);

    UPDATE: -In your while loop of sort method, add smaller = 0; as the first statement. To re-initialize it to 0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following code, which enroll method is better and why? Or should this
This should be a pretty straightforward question. I have the following code, which forms
This code should i place this code at bottom of body it's in conditional
In theory, this code should set the new lock status to the users and
So, i need to have a blinking title, this code should work, but for
This is a snippet of a user invite code. The code should get the
EDIT: My main code no longer works, should this function work? <script type=text/javascript src=jquery-1.7.2.js></script>
I have this code which should create a splash image with either no animation
This should be super simple. The first code block works, but when I wrap
The Google Analytics setup instructions state: This tracking code snippet should be included in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.