I started programming courses 4 months ago and I can't solve this problem according to it's required order .
I need your help as soon as possible and I think it is easy for you :D
thank's in advanced :)

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.
b) What is the fundamental operation in the described algorithm? How many operations does it take?

Recommended Answers

All 4 Replies

This isn't a "we do your homework" service!
Try to answer it yourself. Post your best answer here. Based on that people will give you the help you need.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"

my code (which doesn't work) is :-

public class Assignment1_T7_25_3260_Ibrahem_abdelaziz {
private int[] a;
private int max;
private int n;
int position=0;
    public Assignment1_T7_25_3260_Ibrahem_abdelaziz(int max) {

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

my code (which doesn't work) is :-

You're 95% of the way there...
Add some temporary print statements inside your loops etc so you can see what the values of the variables are as the program executes. That will help you find exactly where it's going wrong.

I think you need "bubble-sort" algorithm for ascending order sort.
Only thing is your sort() function is incorrect.
Below is the modified code for

    public void sort() {
        int x=a.length;
        for(int i=0 ; i< x;i++){
            for(int k=i+1;k<x; k++)
                if(a[i] > a[k]){
                    swap(i, k);

output of the sample run of above code

5 7 3 6 9 

3 5 6 7 9 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.