We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,163 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

how to program this program

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?
Justify.

3
Contributors
4
Replies
3 Days
Discussion Span
7 Months Ago
Last Updated
5
Views
Ibrahem.EzZzat
Newbie Poster
2 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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"
http://www.daniweb.com/community/rules

JamesCherrill
... trying to help
Moderator
8,519 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

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){
    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_T7_25_3260_Ibrahem_abdelaziz;
    trial= new  Assignment1_T7_25_3260_Ibrahem_abdelaziz(maxsize);
    trial.insert(5);
    trial.insert(7);
    trial.insert(3);
    trial.insert(6);
    trial.insert(9);
    trial.display();
    trial.sort();
    trial.display();
}
}

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

Ibrahem.EzZzat
Newbie Poster
2 posts since Oct 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

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.

JamesCherrill
... trying to help
Moderator
8,519 posts since Apr 2008
Reputation Points: 2,583
Solved Threads: 1,455
Skill Endorsements: 30

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 
subramanya.vl
Junior Poster in Training
81 posts since Oct 2012
Reputation Points: 0
Solved Threads: 10
Skill Endorsements: 1

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0876 seconds using 2.69MB