DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Secure APIs: Best Practices and Measures
  • ANEW Can Help With Road Safety
  • How to Measure Product Success
  • 14 Product Success Metrics to Measure Software Development Performance

Trending

  • 5 Web3 Trends to Follow in 2023
  • Send Your Logs to Loki
  • Four Ways for Developers To Limit Liability as Software Liability Laws Seem Poised for Change
  • Memory Management in Java: An Introduction

A Quick Measure of Sortedness

Steve Hanov user avatar by
Steve Hanov
·
Sep. 19, 14 · Interview
Like (0)
Save
Tweet
Share
7.40K Views

Join the DZone community and get the full member experience.

Join For Free

how do you measure the "sortedness" of a list? there are several ways. in the literature this measure is called the "distance to monotonicity" or the "measure of disorder" depending on who you read. it is still an active area of research when items are presented to the algorithm one at a time. in this article, i consider the simpler case where you can look at all of the items at once.

the kendall distance between two lists is the number of swaps it would take to turn one list into another. so, for [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and [10, 1, 2, 3, 4, 5, 6, 7, 8, 9], it would take nine swaps.

edit distance is another method. we could take the 10, and move it after the 9, in one operation. the edit distance is inversely related to the longest increasing subsequence. in the list [1, 2, 3, 5, 4, 6, 7, 9, 8], the longest increasing subsequence is [1, 2, 3, 5, 6, 7, 9], of length seven, and it is three away from being a sorted list. the longest increasing subsequence can be calculated in o(nlogn) time. a drawback of this method is its large granularity. for a list of ten elements, the measure can only take the distinct values 0 through 9.

here, i propose another measure for sortedness. the procedure is to sum the difference between the position of each element in the sorted list, x, and where it ends up in the unsorted list, f(x). we divide by the square of the length of the list and multiply by two, because this gives us a nice number between 0 and 1. subtracting from 1 makes it range from 0, for completely unsorted, to 1, for completely sorted.

a simple genetic algorithm in python for sorting a list using the above fitness function is presented below.

import random

def procreate(a):
    a = a[:]
    first = random.randint(0, len(a) - 1)
    second = random.randint(0, len(a) - 1)
    a[first], a[second] = a[second], a[first]
    return a

def score(a):
    diff = 0.
    for index, element in enumerate(a):
        diff += abs(index - element)

    return 1.0 - diff / len(a) ** 2 * 2

def genetic(root, procreatefn, scorefn, generations = 1000, children=6):
    maxscore = 0.
    for i in range(generations):
        print("generation {0}: {1} {2}".format(i, maxscore, root))
        maxchild = none
        for j in range(children):
            child = procreate(root)
            score = scorefn(child)
            print("    child score {0:.2f}: {1}".format(score, child))
            if maxscore < score:
                maxchild = child
                maxscore = score
        if maxchild:
            root = maxchild
    return root

a = [a for a in range(10)]
random.shuffle(a)
genetic(a, procreate, score)

note that under this metric, the completely reversed list does not have a score of 0.

the spearman's coefficient , mentioned in the comments, might be what you are looking for.

Measure (physics)

Published at DZone with permission of Steve Hanov, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Secure APIs: Best Practices and Measures
  • ANEW Can Help With Road Safety
  • How to Measure Product Success
  • 14 Product Success Metrics to Measure Software Development Performance

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: