PGS
PGS

Reputation: 1174

Sorting/grouping odd and even numbers in odd and even segregated list

I have a unsorted list of number with even and odd. I need to segregate odd and even numbers in sorted order.

For example:

List = [5,6,4,7,11,14,12,1,3]

Expected output :

[4,6,12,14,1,3,5,7,11]

My program to segregate the odd and even numbers.

L = [5,6,4,7,11,14,12,1,3]
def segregateEvenOdd(L):
    left,right = 0,len(L)-1
    while left < right:
        while (L[left]%2==0 and left < right):
            left += 1
        while (L[right]%2 == 1 and left < right):
            right -= 1
        if (left < right):
            L[left],L[right] = L[right],L[left]
            left += 1
            right = right-1

print segregateEvenOdd(L)

output : [12, 6, 4, 14, 11, 7, 5, 1, 3]

I am trying to sort the list using insertion sort, couldn't get right output. Any way to sort this easily

Upvotes: 5

Views: 16165

Answers (12)

Y Logs
Y Logs

Reputation: 64

My approach (long):

 l=[4,1,2,3]

 # separate odd and even position elements
 ev=[]
 odd=[]
 for i in range(len(l)):
     if i%2==0:
         ev.append(l[i])
     else:
         odd.append(l[i])

 # sort them
 newev = sorted(ev)
 newodd= sorted(odd, reverse=True)

 # combine them
 comb=list(zip(newev, newodd))

 # append according to the order
 out=[]
 for i, v in enumerate(op):
     out.append(v[0])
     out.append(v[1])

 print(out)
 # [2,3,4,1]

Upvotes: 1

fferri
fferri

Reputation: 18940

Using a key function for list.sort / sorted:

>>> list(sorted(lst, key=lambda x: [x % 2, x]))
[4, 6, 12, 14, 1, 3, 5, 7, 11]

maps even numbers n to the value [0, n], and odd numbers n to the value [1, n], so that even numbers come first according to natural ordering of list items, i.e. [0, ...] comes before [1, ...].

Upvotes: 8

Shijith
Shijith

Reputation: 4872

Adding to @fferi 's answer.
if you want even numbers followed by odd numbers , each in ascending order, do:

>>> lst=range(10)
>>> sorted(lst, key = lambda x:(x%2, x))
[0, 2, 4, 6, 8, 1, 3, 5, 7, 9]

odd numbers followed by even numbers , each in ascending order

>>> sorted(lst, key = lambda x:(not x%2, x))
[1, 3, 5, 7, 9, 0, 2, 4, 6, 8]

even numbers followed by odd numbers , each in descending order

>>> sorted(lst, key = lambda x:(not x%2, x), reverse=True)
[8, 6, 4, 2, 0, 9, 7, 5, 3, 1]

odd numbers followed by even numbers , each in descending order

>>> sorted(lst, key = lambda x:(x%2, x), reverse=True)
[9, 7, 5, 3, 1, 8, 6, 4, 2, 0]

Upvotes: 5

praveen kedar
praveen kedar

Reputation: 188

In python :

data = [100, 1, 2, 3, 4, 5, 6, 65, 89, 7, 8, 9, 10]
print(sorted([e for e in data if e % 2 == 0]) + sorted([e for e in data if e % 2 != 0]))

Upvotes: 1

Achu
Achu

Reputation: 31

Short_list =[5,7,3,2,8,1,0,10,9,4,6]
def sort_list(my_list):
  even_list = []
  odd_list = []
  for i in my_list:
      if i % 2 == 0:
          even_list.append(i)
      else:
          odd_list.append(i)
  even_list.sort(),odd_list.sort()
  even_list.extend(odd_list)
  return even_list

print(sort_list(Short_list))

[0, 2, 4, 6, 8, 10, 1, 3, 5, 7, 9]

Upvotes: 3

Michael Chinchay
Michael Chinchay

Reputation: 11

the process involve to split the values into even and odd numbers in a separate temporary list, then sort them numerically in a ascending order and finally putting together all in one list. The pictures below shows two projects on C#, one with all the process while the second one is more practical but the process works for your project.

Steps:

  • Split the original list in two, one for even and another for odds.
  • With Both List, Sort them in ascending order.
  • Now Merge both list in one list.

Picture-1

Picture-2

Picture-3

Picture-4

  • This is all the C# Code, I hope this helps, Better Late than Never:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            combodata1();
        }

        public void combodata1() {
            combo1.DataSource = new String[] {"5","10","15","20","25","30","35","40"};
        }

        private void button2_Click(object sender, EventArgs e)
        {
            generate();
        }

        List<String> li1 = new List<String>();
        List<String> li2 = new List<String>();
        List<String> li3 = new List<String>();
        List<String> li4 = new List<String>();
        List<String> li5 = new List<String>();

        public void generate() {

            li1.Clear();

            int r = Convert.ToInt32(combo1.SelectedValue)-1;

            Random ran = new Random();

            for(int i=0;i<=r;i++){

                int num1 = ran.Next(100);
                li1.Add(Convert.ToString(num1));

                                 }

            lista1.DataSource = null;
            lista1.DataSource = li1;

                               }

        private void button1_Click(object sender, EventArgs e)
        {
            String te1 = data1.Text;

            if (te1.Equals("") == false)
            {
                add();
            }

            else {
                MessageBox.Show("- You need to add integer numbers only, empty space or letters are not allowed.","Message");
            }
        }

        public void add() {
            String dato = data1.Text;
            li1.Add(dato);
            lista1.DataSource = null;
            lista1.DataSource = li1;
            data1.Text = "";
                              }

        private void button3_Click(object sender, EventArgs e)
        {          
            int n = lista1.Items.Count;

            if(n>0){
            split();
                   }

            else {
                MessageBox.Show("- Original List must present values.","Message");
                 }
        }

        public void split() {

            int n = lista1.Items.Count-1;

            for (int i = 0; i <= n;i++){

                double a = Convert.ToDouble(li1.ElementAt(i));
                double b = (a / 2);
                double c = b - Math.Floor(b);

                if (c == 0){
                    li2.Add(Convert.ToString(a));
                           }

                else {
                    li3.Add(Convert.ToString(a));
                     }

                                       }

            sort_evenly();

                              }

        public void sort_evenly() {

            int w = li2.Count;

            if (w > 0)
            {

                int n = li2.Count - 1;

                int a = Convert.ToInt32(li2.ElementAt(0));

                for (int i = 1; i <= n; i++)
                {

                    int b = Convert.ToInt32(li2.ElementAt(i));

                    if (a > b) { a = b * 1; }
                    if (a < b) { a = a * 1; }

                }

                li4.Add(Convert.ToString(a));
                li2.Remove(Convert.ToString(a));

                sort_evenly();

            }

            else {
                sort_oddly();
                 }
                                  }

        public void sort_oddly() {

            int w = li3.Count;

            if (w > 0)
            {

                int n = li3.Count - 1;

                int a = Convert.ToInt32(li3.ElementAt(0));

                for (int i = 1; i <= n; i++)
                {

                    int b = Convert.ToInt32(li3.ElementAt(i));

                    if (a < b) { a = a * 1; }
                    if (a > b) { a = b * 1; }

                }

                li5.Add(Convert.ToString(a));
                li3.Remove(Convert.ToString(a));

                sort_oddly();

            }

            else {
               sortedlist();
                 }
                                    }

        String even = "";
        String odd = "";
        String result = "";

        public void sortedlist() {

            even = "";
            odd = "";
            result = "";

            int n = li4.Count-1;
            int k = li5.Count-1;

            for(int i=0;i<=n;i++){
            String hyphen = "-";
            if(i==n){hyphen="";}
            even = even + Convert.ToString(li4.ElementAt(i)) + hyphen;
                                 }

            for(int i=0;i<=k;i++){
            String hyphen = "-";
            if(i==k){hyphen="";}
            odd = odd + Convert.ToString(li5.ElementAt(i)) + hyphen;
                                 }

            result = even + "-" + odd;

            res1.Text = result;

            li2.Clear();
            li3.Clear();
            li4.Clear();
            li5.Clear();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            clear();
        }

        public void clear() {
            li2.Clear();
            li3.Clear();
            li4.Clear();
            li5.Clear();
            res1.Text = "";
                               }

        private void button5_Click(object sender, EventArgs e)
        {
            clearall();
        }

        public void clearall() {
            data1.Text = "";
            li1.Clear();
            li2.Clear();
            li3.Clear();
            li4.Clear();
            li5.Clear();
            lista1.DataSource = null;
            res1.Text = "";
                              }
    }
}

Upvotes: 1

Giuseppe Angora
Giuseppe Angora

Reputation: 853

A simple solution:

import numpy as np
l = [5,6,4,7,11,14,12,1,3]
l_sort = np.sort(l) #sorting elements of the list
evens = list(filter(lambda x: x%2==0, l_sort)) #extract even elements 
odds = list(filter(lambda x: x%2!=0, l_sort)) #extract odd elements
out = evens + odds 

Upvotes: 1

M.H Mighani
M.H Mighani

Reputation: 198

i suggest this if you want to avoid using external libraries:

def even_odd_sort(list):
evens=[]
odds=[]
for i in list:
    if(i%2==0):
        evens.append(i)
    else:
        odds.append(i)
evens.sort()
odds.sort()
return evens+odds

Upvotes: 1

jpp
jpp

Reputation: 164623

If you are happy to use a 3rd party library, you can use Boolean indexing with numpy.

numpy.lexsort sorts in reverse fashion, i.e. it considers A % 2 before A:

import numpy as  np

A = np.array([4,6,12,14,1,3,5,7,11])

res = A[np.lexsort((A, A % 2))]

# [ 4  6 12 14  1  3  5  7 11]

Related: Why NumPy instead of Python lists?

Upvotes: 1

Thierry Lathuille
Thierry Lathuille

Reputation: 24232

We can first sort on n%2 (n modulo 2), which will be 0 for odd numbers and 1 for even ones, then on the number itself:

L = [5,6,4,7,11,14,12,1,3]
out = sorted(L, key = lambda n:(n%2, n))

print(out)
# [4, 6, 12, 14, 1, 3, 5, 7, 11]

The tuples we use as a key are sorted according to their first item first, then to their second.

And it will also work for negative numbers...

Upvotes: 2

Austin
Austin

Reputation: 26039

Make a list of evens and list of odds and then combine:

lst = [5,6,4,7,11,14,12,1,3]
even = sorted([i for i in lst if i%2 == 0])
odd = sorted([i for i in lst if i%2])
print(even + odd)

Or using filter, lambda:

lst = [5,6,4,7,11,14,12,1,3]
lst.sort()

even = list(filter(lambda x: not x%2, lst))
odd = list(filter(lambda x: x%2, lst))

print(even + odd)

Upvotes: 1

ProgSnob
ProgSnob

Reputation: 493

Simply use list comprehension basics

>>> arr = [5,6,4,7,11,14,12,1,3]
>>> evens = sorted([e for e in arr if e % 2 ==0])
>>> odds = sorted([e for e in arr if e % 2 !=0])
>>> print(evens + odds)
[4, 6, 12, 14, 1, 3, 5, 7, 11]

Upvotes: 2

Related Questions