Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4244490
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:42:53+00:00 2026-05-21T03:42:53+00:00

I wrote a script in python that scans a file and extract strings from

  • 0

I wrote a script in python that scans a file and extract strings from it in a big array.
I do something like:

while (delimiter #1 found)
search for the delimiter #2
if the string between #1 and #2 is not in the "final array", add it.

It took me 1 hour to make the script in python.
But it’s just too slow for big files (8 minutes for 400 files is far too long)
So I decided to write this batch in C. After one day I still haven’t finished it.

I’ve already looked at things like sorted arrays (gnu C sorted arrays)
I’d like to check whether the string betwen #1 and #2 is already in an array of strings, and if not, add it.
I thought there would be obvious functions like adding a string in a pre-sorted array (and keep it sorted), and / or adding a string in a pre-sorted array if it’s not already in.

The only solutions I’ve found is

  1. use lsearch()
  2. use bsearch (), and if not found, add it and re-sort the array()

The second function takes ages ( qsort() is too long) and the first one is getting too long after thousand of elements (because they’re not sorted).

Do you know where I could look / what I could do / which library I could use? I guess I’m not the only one on earth who wants to put a string in a pre-sorted string array only if it’s not present (and keep it sorted)! 😉

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:42:54+00:00Added an answer on May 21, 2026 at 3:42 am

    I don’t know of a library for Ansi C to do this, but it’s not that hard to implement yourself. You want to write a “sorted array list” for strings. I’ll give a short idea what this would be looking like:

    struct SortedArrayList {
        int size;
        int capacity;
        char **element;
    }
    
    // returns: >= 0 if the element in contained, < 0 (-insertPos-1) if not
    int GetIndexPos(char *text)
    {
        if (size == 0) return -1;
    
        // Binary search through the list of strings
        int left = 0, right = size-1, center;
        int cmp;
    
        do {
            center = (left+right) / 2;
            cmp = strcmp(element[center],text);
            if (cmp == 0) return center; // found
            if (cmp < 0) left = center+1; // continue right
            else right = center-1; // continue left
        } while (left <= right);
        return -left-1; // not found, return insert position
    }
    
    void Add(char *text)
    {
        int pos = GetIndexPos(text);
        if (pos >= 0) return; // already present
        pos = -pos-1
    
        // Expand the array
        size++;
        if (size >= capacity)
        {
            capacity *= 2;
            element = (char**)realloc(element,capacity*sizeof(char*));
        }
    
        // Add the element at the correct position
        if (pos < size-1) memmove(&element[pos+1],&element[pos],sizeof(char*)*(size-pos-1));
        element[pos] = text;
    }
    

    This will give you complexity of O(log(n)) for sorted insertion with duplicate check. If you want to improve the runtime some more, you can use better data structures as hash maps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a script in Python 2.6.2 that scans a directory for SVG's and
I have a Python script I recently wrote that I call using the command
I wrote a Python script that does some task to generate, and then keep
I wrote a python script that depends on a certain NFS share to be
I'm writing a python script that uses subprocess.Popen to execute two programs (from compiled
I'm trying to write a python script that packages our software. This script needs
I would like to write some scripts in python that do some automated changes
I wrote a script in Ruby. I'd like to run it every day at
So I wrote this short script (correct word?) to download the comic images from
I wrote a short bash script to complete a task that involves creating a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.