Sorting a list of zeros and ones.

  • Thread starter MathematicalPhysicist
  • Start date
  • Tags
    List Sorting
In summary, the conversation discusses generating a truth table with n-2 columns and 2^(n-2) rows, where the initial entries are all zeros. The process involves using nested for loops and complicated conditions, but there are simpler approaches available. The individual suggests using a single for loop and masks to extract bits from a counter.
  • #1
MathematicalPhysicist
Gold Member
4,699
371
Assume i have an initial list of n-2 columns and 2^(n-2) rows, where initially:
all the entries are zero.
i wnt to sort it out that eveantually we'll get a truth table for this.
i.e if we have n-2=2 rows and 4 columns, then:
Code:
0 0
1 0
0 1
1 1
obvously we have here a jump first we initialise the array to give us everywhere zero, afterwards i need to insert via a loop the entries of 1, i.e in the first column it should be changing the zeros in a[1][1] and a[3][1] i.e we have a jump of two places and in the next column a jump of three places, but i don't know how to write it in the loop condition, any thoughts?
the exam is tommorow, so your help would be tremendous for my success, thanks in advance.
 
Technology news on Phys.org
  • #2
You mean you want to generate something like this:
Code:
... 0 0 0 0
... 0 0 0 1
... 0 0 1 0
... 0 0 1 1
... 0 1 0 0
... 0 1 0 1
... 0 1 1 0
... 0 1 1 1
... 1 0 0 0
... 1 0 0 1
... 1 0 1 0
... 1 0 1 1
... 1 1 0 0
... 1 1 0 1
... 1 1 1 0
... 1 1 1 1
... . . . .
    . . . .
    . . . .
?
 
  • #3
Is there some particular reason you need to do this with nested for loops and complicated conditions? There are many simpler approaches.

- Warren
 
  • #4
yes job, something like that.


chroot, iv'e learned loops, so i guess this is the way i shouldv'e solved it.

thanks anyway.
 
  • #5
If I were doing it, I'd just use a single for loop that increments a counter. To extract each bit from the counter, I'd use a set of masks, one for each bit.

- Warren
 

1. How do you sort a list of zeros and ones?

To sort a list of zeros and ones, you can use a sorting algorithm such as quicksort or mergesort. These algorithms work by comparing the values of the elements in the list and rearranging them in ascending or descending order.

2. Can you use bubble sort to sort a list of zeros and ones?

Yes, bubble sort can be used to sort a list of zeros and ones. However, bubble sort is not the most efficient sorting algorithm for this task, as it has a time complexity of O(n^2).

3. Is it possible to sort a list of zeros and ones in linear time?

Yes, it is possible to sort a list of zeros and ones in linear time using a counting sort algorithm. This algorithm works by counting the number of zeros and ones in the list and then reconstructing the list in sorted order.

4. How do you handle duplicate values when sorting a list of zeros and ones?

When sorting a list of zeros and ones, duplicate values can be handled by using a stable sorting algorithm. This ensures that the relative order of duplicate values is preserved in the sorted list.

5. Can you sort a list of zeros and ones in-place?

Yes, it is possible to sort a list of zeros and ones in-place by using an in-place sorting algorithm such as quicksort or heapsort. These algorithms rearrange the elements within the original list, rather than creating a new sorted list.

Similar threads

  • Programming and Computer Science
Replies
7
Views
422
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
853
  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
4
Views
912
Replies
9
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Precalculus Mathematics Homework Help
Replies
1
Views
525
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top