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

# 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

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
Skill Endorsements: 0

This isn't a "we do your homework" service!

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
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
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
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
Skill Endorsements: 1

Post: Markdown Syntax: Formatting Help

You