include data from txt

i was modify some source code
i want to include the Data from some. txt?
how to add function?
================================================================================
#include <iostream>
#include <time.h>

using namespace std;

int data[100000000], data_2[100000000];
int n; /* n = amount of data*/

int input(){
cout<<"Input n data: ";
cin>>n;
for(int i=0; i<n; i++){
cout<<"Data -" <<(i+1)<<" : ";
cin>>data[i];
data_2[i]=data[i];
}
cout<<endl;
}

void swap(int x, int y){
int temp;
temp=data[y];
data[y]=data[x];
data[x]=temp;
}

void selection_sort(){
int pos;
clock_t start = clock();
for(int i=0; i<n-1; i++){
pos=i;
for(int j=i+1; j<n; j++){
if(data[j]<data[pos])
pos=j;
}
if(pos!=i)
swap(pos,i);
}
cout<<"Sorting Finished!!!";
cout<<endl;
clock_t ends = clock();
cout << "Running Time : " << (double) (ends - start) / CLOCKS_PER_SEC;
cout<<endl;
}


void insertion_sort(){
int temp,j;
clock_t start = clock();
for(int i=1; i<n; i++){
temp=data[i];
j=i-1;
while(data[j]>temp && j>=0){
data[j+1] = data[j];
j--;
}
data[j+1]=temp;
}
cout<<"Sorting Finished!!!";
cout<<endl;
clock_t ends = clock();
cout << "Running Time : " << (double) (ends - start) / CLOCKS_PER_SEC ;
cout<<endl;
}

void buble_sort(){
int t;
clock_t start = clock();
for(int i=1; i<n; i++){
for(int j=n-1; j>=i; j--){
if (data[j]<data[j-1])
swap(j,j-1);
}
}
cout<<"Sorting Finished!!!";
cout<<endl;
clock_t ends = clock();
cout << "Running Time : " << (double) (ends - start) / CLOCKS_PER_SEC ;
cout<<endl;
}

void Quicksort(int p, int r){
int i, j, mid;
i=p;
j=r;
mid=data[(p+r)/2];

do{
while (data[i]<mid)
i++;
while (data[j]>mid)
j--;

if(i<=j){
swap(i,j);
i++;
j--;
}
}
while(i<j);

if(p<j)
Quicksort(p,j);

if (i<r)
Quicksort(i,r);
}
void running_time_Quick_sort(){
clock_t start = clock();
Quicksort(0, n-1);
cout<<"Sorting Finished!!!";
cout<<endl;
clock_t ends = clock();
cout << "Running Time : " << (double) (ends - start) / CLOCKS_PER_SEC ;
cout<<endl;
}
void m_sort(int B[],int left,int middle,int right)
{
int bag_left,postemp,j,i;
int temp[20];
bag_left=middle-1;
postemp=left;
j=right-left+1;
while((left<=bag_left)&&(middle<=right))
{
if(B[left]<=B[middle])
{
temp[postemp]=B[left];
postemp=postemp+1;
left=left+1;
}
else
{
temp[postemp]=B[middle];
postemp=postemp+1;
middle=middle+1;
}
}
while(left<=bag_left)
{
temp[postemp]=B[left];
postemp=postemp+1;
left=left+1;
}
while(middle<=right)
{
temp[postemp]=B[middle];
postemp=postemp+1;
middle=middle+1;
}
cout<<endl;
for(i=1;i<=j;i++)
{
B[right]=temp[right];
cout<<"\nB["<<right<<"] : "<<B[right];
right=right-1;
}
cout<<endl;
}

void merge_sort(int A[],int left,int right)
{
int middle;
if(left<right)
{
middle=(left+right)/2;
merge_sort(A,left,middle);
merge_sort (A,middle+1,right);
m_sort(A,left,middle+1,right);
}
}

void running_time_Merge_sort(){
clock_t start = clock();
merge_sort (data,0,n-1);
cout<<"Sorting Finished!!!";
cout<<endl;
clock_t ends = clock();
cout << "Running Time : " << (double) (ends - start) / CLOCKS_PER_SEC ;
cout<<endl;
}

void print(){
for(int i=0;i<n;i++)
{
cout<<data[i]<<" ";
}
cout<<endl<<endl;
}

int main (){
int pil;
do{
cout<<"1. Selection sort";
cout<<endl;
cout<<"2. Insertion sort";
cout<<endl;
cout<<"3. Buble sort";
cout<<endl;
cout<<"4. Quick sort";
cout<<endl;
cout<<"5. Merge sort";
cout<<endl;
cout<<"6. print";
cout<<endl;
cout<<"7. Exit";
cout<<endl;
cout<<"Pilihan : ";
cin>>pil;
cout<<endl;
switch(pil){
case 1:
input();
selection_sort();
cout<<endl;
break;
case 2:
input();
insertion_sort();
cout<<endl;
break;
case 3:
input();
buble_sort();
cout<<endl;
break;
case 4:
input();
Quicksort(0, n-1);
running_time_Quick_sort();
cout<<endl;
break;
case 5:
input();
merge_sort (data,0,n-1);
running_time_Merge_sort();
cout<<endl;
break;
case 6:
print();
break;

}
}
while(pil!=7);
return 0;
}
Last edited on
I think you should read this:

http://www.cplusplus.com/doc/tutorial/files/
Topic archived. No new replies allowed.