Home > Net >  Is it possible to loop through some numbers in a file and check if they are sorted without the need
Is it possible to loop through some numbers in a file and check if they are sorted without the need

Time:11-29

I would like to know if I can perform operations on some numbers straight in the file without

the need to read them. I wrote this code to check if a file is sorted but I had to read them

first into a vector and then check if the vector is sorted or not but then I figured that this

code might be inefficient since I had to make few extra steps. this is the code:

// method to check if the numbers are already sorted:

bool number_sorted(vector <int> vector){
    bool is_sorted = true;
     for(int i = 0; i < vector.size(); i  ){
        for(int j = i   1; j < vector.size(); j  ){
            if(vector[i] > vector[j]){
                is_sorted = false;  
                cout << vector[i] << " and " << vector[j] << " are in the wrong order" << endl; 
            }
        }
    } 
    return is_sorted;
}

// method to sort the numbers:

vector <int> sort(vector <int> vector){
    for(int i = 0; i < vector.size(); i  ){
        for(int j = i   1; j < vector.size(); j  ){
            if(vector[i] > vector[j]){
                int temp = vector[i]; 
                vector[i] = vector[j]; 
                vector[j] = temp; 
            }
        }
    }

    return vector; 
}

// Main methdod:

int main(){
    vector <int> list; 
    fstream fs;
   fs.open("/Users/brah79/Downloads/skola/c  /inlämningsuppgiter/number1.txt"); 
    
    bool is_sorted = number_sorted(list); 
    if(is_sorted){
        cout << "the list of numbers is sorted" << endl; 
    }

    else{
        sort(list); 
    }

as you can see everything is performed on a vector first but I want to make the check and the

sorting straight on the file. Hope I made myself clear

CodePudding user response:

You have to read file there is not other way. But you do not have to keep everything in vector

bool areIntsInStreamSorted(std::istream& in)
{
    return std::is_sorted(std::istream_iterator<int>{in}, {}) && in.eof();
}

bool areIntsInFileSorted(std::filesystem::path p)
{
    std::ifstream in{p};
    return areIntsInStreamSorted(in);
}

CodePudding user response:

Lets look at your implementation:

bool number_sorted(vector <int> vector){
    bool is_sorted = true;
     for(int i = 0; i < vector.size(); i  ){
        for(int j = i   1; j < vector.size(); j  ){
            if(vector[i] > vector[j]){
                is_sorted = false;  
                cout << vector[i] << " and " << vector[j] << " are in the wrong order" << endl; 
            }
        }
    } 
    return is_sorted;
}

You consider every element vector[i] and then check for all j > i that vector[i] is not greater than vector[j]. If it is, the vector is not sorted. If you you encounter no such pair of elements vector[i] and vector[j] then the vector is sorted.

If this was the way to check if a sequence of numbers is sorted, then yes, then you need all numbers in memory.

However, it is not. It is much simpler. In a sequence that is not sorted, there is at least one index i where

 vector[i] > vector[i 1]

and thats it. If any pair of adjacent elements is sorted, then the whole sequence is sorted too.

You only ever have to compare one number with the next. You do not need to store all elements to check if they are sorted. When you read from the file, remember the previous number, read the next, and check if the next is bigger or equal the previous one.

  • Related