Changes

From Genome Analysis Wiki
Jump to navigationJump to search
Line 56: Line 56:  
** [[Media:Rand-1M-3digits.txt.gz| (Example data - Rand-1M-3digits.txt.gz)]] 1,000,000 random data from 1 to 1,000]] (gzipped)  
 
** [[Media:Rand-1M-3digits.txt.gz| (Example data - Rand-1M-3digits.txt.gz)]] 1,000,000 random data from 1 to 1,000]] (gzipped)  
 
** [[Media:Rand-50k.txt.gz | (Example data - Rand-50k.txt.gz)]] 50,000 random data from 1 to 1,000,000)]] (gzippd)
 
** [[Media:Rand-50k.txt.gz | (Example data - Rand-50k.txt.gz)]] 50,000 random data from 1 to 1,000,000)]] (gzippd)
** Source code from Problem 3  
+
 
 +
=== Source code from Problem 3 ===
 
  #include <iostream>
 
  #include <iostream>
 
  #include <vector>
 
  #include <vector>
Line 148: Line 149:       −
Here is the header file of mySortedArray.h
+
=== The header file of mySortedArray.h ===
    
  #include <iostream>
 
  #include <iostream>
Line 238: Line 239:  
     return false;    // cannot find the value to remove
 
     return false;    // cannot find the value to remove
 
   }
 
   }
 +
}
 +
 +
 +
=== Example code to generare all possible permutations ===
 +
// Code to generate permutations from 1..n
 +
// These code was adopted from
 +
// http://geeksforgeeks.org/?p=767 by Hyun Min Kang
 +
#include <iostream>
 +
#include <vector>
 +
 +
// swaps two elements
 +
void swap(std::vector<int>& v, int i, int j) {
 +
  int tmp = v[i];
 +
  v[i] = v[j];
 +
  v[j] = tmp;
 +
  return;
 +
}
 +
 +
// actual engine - recursively calls permutation
 +
void permute(std::vector<int>& v, int from, int to) {
 +
  if ( from == to ) {          // print the permutation
 +
    for(int i=0; i < v.size(); ++i) {
 +
      std::cout << " " << v[i];
 +
    }
 +
    std::cout << std::endl;
 +
  }
 +
  else {
 +
    for(int i = from; i <= to; ++i) {
 +
      swap(v,from,i);          // swaps two elements
 +
      permute(v, from+1, to);  // permute the rest
 +
      swap(v,from,i);          // recover the vector to the original state
 +
    }
 +
  }
 +
}
 +
 +
// Permutation Example
 +
int main(int argc, char** argv) {
 +
  if ( argc != 2 ) {
 +
    std::cerr << "Usage: " << argv[0] << " " << "[# of samples]" << std::endl;
 +
    return -1;
 +
  }
 +
 +
  // takes input from 1..n
 +
  int n = atoi(argv[1]);
 +
  std::vector<int> input;
 +
  for(int i=1; i <= n; ++i) {
 +
    input.push_back(i);
 +
  }
 +
  permute(input, 0, n-1); // print all possible permutations
 +
  return 0;
 
  }
 
  }
  

Navigation menu