# Any one can help me on this code

**URL:** https://discourse.julialang.org/t/any-one-can-help-me-on-this-code/28251
**Category:** New to Julia
**Tags:** question
**Created:** [September 1, 2019, 12:26am UTC](https://discourse.julialang.org/t/any-one-can-help-me-on-this-code/28251 "2019-09-01T00:26:02Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)
#### Post date: [September 1, 2019, 12:26am UTC](https://discourse.julialang.org/t/any-one-can-help-me-on-this-code/28251/1 "2019-09-01T00:26:02Z")

</div>

```julia

#this method sort the array that all 
#the even number in the first and the odd 
#number in the even, and reutnr #the number os swap, 
#e.g. in array = [17,4,8], there is a 17 odd number and 8 #even number. swaping so swapping them and the move is 1
function sort_even_odd(data)
	left = 1
	right = length(data)
	moves = 0
while left < right
	  	while iseven(data[left]) && left < right
	    	left += 1
	    end	  	
	    while isodd(data[right]) && left < right
	    	right -= 1
	    end 
	  	
	  	if left < right 
	    	data[left],data[right] = data[right],data[left]
	    	left += 1
	    	right -= 1
	    	moves += 1
	  	end 
	end
	#data, moves 
  moves
end 
data = [17 4 8];
println("Total " , sort_even_odd(data))

```

---

<div class="post-metadata">

### Author: ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)
#### Post date: [September 1, 2019, 12:47am UTC](https://discourse.julialang.org/t/any-one-can-help-me-on-this-code/28251/2 "2019-09-01T00:47:30Z")

</div>

FYI, it looks like you’re using quoting intended for speech rather than code. You want to use 3 backticks at the top and bottom of code blocks or press the button that looks like `</>`. Compare

> function foo(speech)  
> println(“this doesn’t look good”)  
> end

to

```julia
function bar(code)
    println("ahh, much better")
end

```

You can still edit your post to fix it.

You should probably also say more about what your specific problem is, what else you’ve tried, etc. [See this post](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) for more information on how to write a question in a way that helps others to help you.

---

<div class="post-metadata">

### Author: ![Seif\_Shebl](https://avatars.discourse-cdn.com/v4/letter/s/eada6e/32.png) [@Seif\_Shebl](https://discourse.julialang.org/u/Seif_Shebl)
#### Post date: [September 1, 2019, 3:19am UTC](https://discourse.julialang.org/t/any-one-can-help-me-on-this-code/28251/3 "2019-09-01T03:19:17Z")

</div>

Without using fancy stuff, here is a simple implementation of the algorithm:

1. Initialize two index variables left and right:  
left = 1, right = size
2. Keep incrementing left index until we see an odd number.
3. Keep decrementing right index until we see an even number.
4. If left \< right then swap data[left] and data[right]

```julia
function sort_even_odd(data)
	left = 1
	right = length(data)
	moves = 0

	while left < right		
	  	while iseven(data[left]) && left < right
	    	left += 1
	    end	  	
	    while isodd(data[right]) && left < right
	    	right -= 1
	    end 
	  	
	  	if left < right 
	    	data[left],data[right] = data[right],data[left]
	    	left += 1
	    	right -= 1
	    	moves += 1
	  	end 
	end
	data, moves 
end 

julia> data = [17 4 8];

julia> sort_even_odd(data)
([8 4 17], 1)

```

---

<div class="post-metadata">

### Author: ![Fuad\_Sami](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fuad_sami/32/16900_2.png) [@Fuad\_Sami](https://discourse.julialang.org/u/Fuad_Sami)
#### Post date: [September 1, 2019, 9:26am UTC](https://discourse.julialang.org/t/any-one-can-help-me-on-this-code/28251/4 "2019-09-01T09:26:59Z")

</div>

thanks dear,  
its working perfect.

i did not know that i can making the swap like this → data[left],data[right] = data[right],data[left]
