Lecture 18

  • April 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Lecture 18 as PDF for free.

More details

  • Words: 1,307
  • Pages: 7
‫‪Data Structures‬‬ ‫‪Notes for Lecture 18‬‬ ‫‪Introduction of Sorting‬‬ ‫‪By‬‬ ‫‪Samaher Hussein Ali‬‬ ‫‪2007-2008‬‬

‫‪Sorting‬‬ ‫اﻟﺘﺮﺗﻴﺐ هﻮ ﻋﻤﻠﻴﺔ ﺗﻨﻈﻴﻢ ﻣﺠﻤﻮﻋﺔ ﻣﻦ اﻟﻌﻨﺎﺻﺮ اﻟﺒﻴﺎﻧﻴﺔ وﻓﻖ ﻗﻴﻤﺔ ﺣﻘﻞ )او ﻣﺠﻤﻮﻋﺔ ﺣﻘﻮل ( ﻳﺴﻤﻰ ﺑﺎﻟﻤﻔﺘﺎح ﺑﺼﻮرة ﺗﺼﺎﻋﺪﻳﺔ او ﺗﻨﺎزﻟﻴﺔ‪.‬‬ ‫واﻟﻐﺮض ﻣﻦ اﻟﺘﺮﺗﻴﺐ هﻮ‪-:‬‬ ‫‪ .1‬ﻟﺰﻳﺎدة آﻔﺎءة ﺧﻮارزﻣﻴﺎت اﻟﺒﺤﺚ ﻋﻦ ﻋﻨﺼﺮ ﻣﺎ‬ ‫‪ .2‬ﻟﺘﺒﺴﻴﻂ ﻣﻌﺎﻟﺠﺔ اﻟﻤﻠﻔﺎت‬ ‫‪ .3‬ﻟﺤﻞ ﻣﺸﻜﻠﺔ ﺗﺸﺎﺑﻪ اﻟﻘﻴﻮد‬ ‫وﻳﻮﺟﺪ ﻋﺪة ﺗﺼﻨﻴﻔﺎت ﻟﺨﻮارزﻣﻴﺎت اﻟﺘﺮﺗﻴﺐ ﻣﻨﻬﺎ‪-:‬‬ ‫‪.1‬ﺧﻮارزﻣﻴﺎت ﺗﻌﺘﻤﺪ اﻟﻤﻘﺎرﻧﺎت‬ ‫‪ .2‬ﺧﻮارزﻣﻴﺎت ﺗﻌﺘﻤﺪ اﻟﺘﻮزﻳﻊ‬ ‫‪ .3‬ﺧﻮارزﻣﻴﺎت ﺗﻌﺘﻤﺪ ﻋﻠﻰ اﺟﻬﺰة اﻟﺨﺰن وﺗﺘﻀﻤﻦ‪-:‬‬ ‫أ‪ .‬ﺧﻮارزﻣﻴﺎت اﻟﺘﺮﺗﻴﺐ اﻟﺪاﺧﻠﻲ‬

‫‪Internal Sort Algorithm‬‬

‫ب‪ .‬ﺧﻮارزﻣﻴﺎت اﻟﺘﺮﺗﻴﺐ اﻟﺪاﺧﻠﻲ‬

‫‪External Sort Algorithm‬‬

‫*ﺧﻄﻮات ﻋﻤﻠﻴﺔ اﻟﺘﺮﺗﻴﺐ‬ ‫ﺧﻮارزﻣﻴﺎت اﻟﺘﺮﺗﻴﺐ ﺑﻤﺨﺘﻠﻒ اﻧﻮاﻋﻬﺎ ﺗﺘﻀﻤﻦ اﻟﺨﻄﻮات اﻟﺘﺎﻟﻴﺔ‬ ‫‪ .1‬ﻗﺮاءة ﺣﻘﻞ اﻟﻤﻔﺘﺎح‬ ‫‪ .2‬اﺳﺘﻨﺘﺎج ﻣﻮﻗﻊ اﻟﻌﻨﺼﺮ ﻓﻲ اﻟﺘﺮﺗﻴﺐ اﻟﺠﺪﻳﺪ‬ ‫‪ .3‬ﻧﻘﻞ اﻟﻌﻨﺼﺮ اﻟﻰ اﻟﻤﻮﻗﻊ اﻟﺠﺪﻳﺪ‬

‫*اﺳﺲ ﻗﻴﺎس آﻔﺎءة ﺧﻮارزﻣﻴﺎت اﻟﺘﺮﺗﻴﺐ‬ ‫‪ .1‬ﻣﻌﺪل ﻣﺎ ﺗﺤﺘﺎﺟﻪ اﻟﺨﻮارزﻣﻴﺔ ﻣﻦ ﻣﻘﺎرﻧﺎت‬ ‫‪ .2‬ﻣﻌﺪل ﻣﺎ ﺗﺤﺘﺎﺟﻪ اﻟﺨﻮارزﻣﻴﺔ ﻣﻦ اﻟﻨﻘﻼت او اﻟﺘﺤﺮﻳﻜﺎت‬ ‫‪ .3‬ﻣﻌﺪل ﻣﺎ ﺗﺤﺘﺎﺟﻪ اﻟﺨﻮارزﻣﻴﺔ ﻣﻦ اﻟﺘﺒﺪﻳﻼت‬ ‫‪ .4‬ﻣﻌﺪل اﻟﺤﺠﻢ اﻟﺘﺨﺰﻳﻨﻲ‬

‫‪Internal Sort Algorithm‬‬ ‫ﺧﻮارزﻣﻴﺎت اﻟﺘﺮﺗﻴﺐ اﻟﺪاﺧﻠﻲ وﻧﻘ ﺼﺪ ﺑﺎﻟﺘﺮﺗﻴ ﺐ اﻟ ﺪاﺧﻠﻲ ﺑﺎﻟﺘﺮﺗﻴ ﺐ اﻟ ﺬي ﻳﺤ ﺪث ﻓ ﻲ اﻟ ﺬاآﺮة اﻟﺮﺋﻴ ﺴﻴﺔ ﻟﻠﺤﺎﺳ ﺒﺔ ﻋﻨ ﺪﻣﺎ ﻳﻜ ﻮن ﺣﺠ ﻢ اﻟﺒﻴﺎﻧ ﺎت‬ ‫ﻣﻨﺎﺳﺒًﺎ )ﻟﻴﺲ آﺒﻴﺮًا( وﺗﺘﻀﻤﻦ هﺬﻩ اﻟﺨﻮارزﻣﻴﺎت ﺧﻤﺲ ﻃﺮق ‪-:‬‬

‫‪1‬‬

‫‪ .1‬ﻃﺮق اﻟﺘﺮﺗﻴﺐ ﺑﺎﺳﺘﺨﺪام اﻟﺘﺒﺪﻳﻼت‬ ‫وﺗﺘﻀﻤﻦ ﻋﺪة ﺧﻮارزﻣﻴﺎت‬ ‫‪Bubble Sort (Exchange)Algorithm‬‬

‫* ﺧﻮارزﻣﻴﺔ اﻟﺘﺮﺗﻴﺐ اﻟﻔﻘﺎﻋﻲ‬

‫‪Quick Sort Algorithm‬‬

‫* ﺧﻮارزﻣﻴﺔ اﻟﺘﺮﺗﻴﺐ اﻟﺴﺮﻳﻊ‬ ‫*ﺧﻮارزﻣﻴﺔ ﺷﻴﻞ‬

‫‪Shell Algorithm‬‬

‫*ﺧﻮارزﻣﻴﺔ ﺷﻴﻜﺮ‬

‫‪Shaker Algorithm‬‬

‫* ﺧﻮارزﻣﻴﺔ اﻻزاﺣﺔ‬

‫‪Shifting Algorithm‬‬

‫* ﺧﻮارزﻣﻴﺔ ﺑﺎﺗﺠﺮ‬

‫‪Batcher Algorithm‬‬

‫* ﺧﻮارزﻣﻴﺔ اﻟﺘﺮﺗﻴﺐ اﻟﻔﺮدي واﻟﺰوﺟﻲ‬

‫‪Add-Even Sort Algorithm‬‬ ‫‪ .2‬ﻃﺮق اﻟﺘﺮﺗﻴﺐ ﺑﺎﻻﺿﺎﻓﺔ‬ ‫وﺗﺘﻀﻤﻦ ﻋﺪة ﺧﻮارزﻣﻴﺎت‬ ‫* اﻟﺘﺮﺗﻴﺐ ﺑﺎﻻﺿﺎﻓﺔ اﻟﻰ اﻟﻘﺎﺋﻤﺔ‬ ‫* اﻟﺘﺮﺗﻴﺐ ﺑﺤﺴﺎب اﻟﻌﻨﻮان‬ ‫* اﻟﺘﺮﺗﻴﺐ ﺑﺎﻻﺿﺎﻓﺔ اﻟﺜﻨﺎﺋﻲ‬ ‫* اﻟﺘﺮﺗﻴﺐ ﺑﺎﻻﺿﺎﻓﺔ اﻟﺨﻄﻲ‬ ‫‪ .3‬ﻃﺮق اﻟﺘﺮﺗﻴﺐ ﺑﺎﺳﺘﺨﺪام اﻟﺪﻣﺞ‬ ‫وﺗﺘﻀﻤﻦ ﻋﺪة ﺧﻮارزﻣﻴﺎت‬ ‫*ﺧﻮارزﻣﻴﺔ اﻟﺪﻣﺞ اﻟﺒﺴﻴﻂ‬ ‫*ﺧﻮارزﻣﻴﺔ اﻟﺪﻣﺞ اﻟﻤﺴﺘﻘﻴﻢ‬ ‫*ﺧﻮارزﻣﻴﺔ اﻟﺪﻣﺞ اﻟﻄﺒﻴﻌﻲ‬ ‫‪ .4‬ﻃﺮق اﻟﺘﺮﺗﻴﺐ اﻟﺘﻮزﻳﻌﻲ‬ ‫وﺗﺘﻀﻤﻦ ﺧﻮارزﻣﻴﺔ واﺣﺪة ﻓﻘﻂ هﻲ‪-:‬‬ ‫* ﺧﻮارزﻣﻴﺔ ﺗﺮﺗﻴﺐ اﻻﺳﺎس‬

‫‪Radix Sort Algorithm‬‬

‫‪ .5‬ﻃﺮق اﻟﺘﺮﺗﻴﺐ ﺑﻮاﺳﻄﺔ اﻻﺧﺘﻴﺎر‬ ‫وﺗﺘﻀﻤﻦ ﻋﺪة ﺧﻮارزﻣﻴﺎت‪-:‬‬ ‫* ﺧﻮارزﻣﻴﺔ اﻻﺧﺘﻴﺎر اﻟﺨﻄﻲ‬ ‫*ﺧﻮارزﻣﻴﺔ اﻻﺧﺘﻴﺎر اﻟﺘﺮﺑﻴﻌﻲ‬ ‫*ﺧﻮارزﻣﻴﺔ اﻟﺘﺮﺗﻴﺐ اﻟﻜﻮﻣﻲ‬ ‫*ﺧﻮارزﻣﻴﺔ اﻟﺘﺮﺗﻴﺐ اﻟﺸﺠﺮي‬ ‫*ﺧﻮارزﻣﻴﺔ اﻻﺧﺘﻴﺎر اﻟﺨﻄﻲ ﺑﺎﻟﺘﺒﺪﻳﻼت‬ ‫*ﺧﻮارزﻣﻴﺔ اﻻﺧﺘﻴﺎر اﻟﺨﻄﻲ ﺑﺎﻟﻌﺪ‬

‫‪2‬‬

‫‪External Sort Algorithm‬‬ ‫ﺧﻮارزﻣﻴﺎت اﻟﺘﺮﺗﻴﺐ اﻟﺨﺎرﺟﻲ ﻓﻜﺮﺗﻬﺎ اذا آﺎن ﻟﺪﻳﻨﺎ آﻤﻴﺔ آﺒﻴﺮة ﻣﻦ اﻟﺒﻴﺎﻧﺎت ﻻﺗﺘﺴﻌﻬﺎ اﻟﺬاآﺮة اﻟﺮﺋﻴﺴﻴﺔ ﻳﺘﻢ ﺧﺰﻧﻬﺎ ﻋﻠﻰ اﺣﺪ وﺳﺎﺋﻞ اﻟﺨﺰن‬ ‫اﻟﺜﺎﻧﻮﻳﺔ ﺛﻢ ﺗﻘﺴﻢ اﻟﻰ ﻣﻘﺎﻃﻊ وﻳﺘﻢ ﺗﺮﺗﻴﺒﻬﺎ وﻓﻖ اﺣﺪى ﻃﺮق اﻟﺘﺮﺗﻴﺐ اﻟﺪاﺧﻠﻲ ﺛﻢ ﻳﺘﻢ اﻟﺪﻣﺞ ﺑﻴﻦ هﺬﻩ اﻟﻤﻘﺎﻃﻊ ‪.‬‬ ‫وﻳﺘﻀﻤﻦ اﻟﺘﺮﺗﻴﺐ اﻟﺨﺎرﺟﻲ اﻟﻄﺮق اﻟﺘﺎﻟﻴﺔ‪-:‬‬ ‫* اﻟﺘﺮﺗﻴﺐ ﺑﺎﻟﺪﻣﺞ ذو اﻟﻤﺴﺎرﻳﻦ‬

‫‪Two-Way-Merge‬‬ ‫‪K-Way-Merge‬‬ ‫‪Balanced Two-Way-Merge‬‬

‫* اﻟﺘﺮﺗﻴﺐ ﺑﺎﻟﺪﻣﺞ ﻣﺘﻌﺪد اﻟﻤﺴﺎرﻳﻦ‬ ‫* اﻟﺘﺮﺗﻴﺐ ﺑﺎﻟﺪﻣﺞ اﻟﻤﺘﻮازي ذي اﻟﻤﺴﺎرﻳﻦ‬

‫‪Poly phase Two-Way-Merge‬‬

‫* اﻟﺘﺮﺗﻴﺐ ﺑﺎﻟﺪﻣﺞ ﻣﺘﻌﺪد اﻻﻃﻮار‬

‫‪Bubble Sort Algorithm‬‬ ‫ﺧﻮارزﻣﻴﺔ اﻟﺘﺮﺗﻴﺐ اﻟﻔﻘﺎﻋﻲ‬ ‫ﻓﻜﺮﺗﻬﺎ‪-:‬‬ ‫هﻲ ﻣﻘﺎرﻧﺔ آﻞ ﻋﻨﺼﺮﻳﻦ ﻣﺘﺠﺎورﻳﻦ ﻓﻲ اﻟﻘﺎﺋﻤﺔ وﺗﺒﺪﻳﻞ ﻣﻮاﻗﻌﻬﻤﺎ وﺧﻼل آﻞ دورة ﻣﻦ اﻟﻤﻘﺎرﻧﺎت واﻟﺘﺒﺪﻳﻼت ﻳﺘﺤﺮك اﻟﻌﻨﺼﺮ ﺣﺘﻰ ﻳﺴﺘﻘﺮ‬ ‫ﻓﻲ اﻟﻤﻮﻗﻊ اﻟﻤﻨﺎﺳﺐ‪.‬‬ ‫ﺣﻴﺚ اﻧﻨﺎ ﻧﺤﺘﺎج اﻟﻰ ) ‪ ( N-1‬ﻣﻦ اﻟﺪورات ﻟﻠﺤﺼﻮل ﻋﻠﻰ ﻋﻤﻠﻴﺔ اﻟﺘﺮﺗﻴﺐ وﺣﻴﺚ ان )‬

‫‪ ( N‬ﻳﻤﺜﻞ ﻋﺪد اﻟﻌﻨﺎﺻﺮ‪.‬‬

‫ﺳﻤﻴﺖ ﺑﺎﻟﺘﺮﺗﻴﺐ اﻟﻔﻘﺎﻋﻲ وذﻟﻚ ﻻن اﻻرﻗﺎم اﻟﺼﻐﻴﺮة ﺗﻄﻔﻮ اﻟﻰ اﻻﻋﻠﻰ واﻻرﻗﺎم اﻟﻜﺒﻴﺮة ﺗﺴﺘﻘﺮ ﻓﻲ اﻻﺳﻔﻞ‪.‬‬ ‫ﻣﺜﺎل ‪ -:‬رﺗﺐ ﻋﻨﺎﺻﺮ اﻟﻘﺎﺋﻤﺔ اﻟﺘﺎﻟﻴﺔ ﺑﺎﺳﺘﺨﺪام اﻟﺘﺮﺗﻴﺐ اﻟﻔﻘﺎﻋﻲ‬ ‫‪43‬‬

‫‪54‬‬

‫‪11‬‬

‫‪41‬‬

‫‪93‬‬

‫‪17‬‬

‫‪5‬‬

‫‪66‬‬

‫اآﺒﺮ ﻋﺪد ﻣﻦ اﻟﻤﻘﺎرﻧﺎت=‬ ‫ﺑﻤﺎ ان ‪ N=8‬ﻟﺬﻟﻚ ﻳﺠﺐ ان ﻻﻳﺰﻳﺪ ﻋﺪد اﻟﻤﻘﺎرﻧﺎت ﻋﻦ ‪ 28‬ﻣﻘﺎرﻧﺔ‬ ‫‪N(N-1)/2‬‬

‫ﺧﻄﻮات اﻟﺤﻞ‪-:‬‬ ‫اﻟﻤﺮﺣﻠﺔ اﻻوﻟﻰ ‪-:‬‬ ‫ﻧﻘﺎرن اﻟﻌﻨﺼﺮ ﻓﻲ اﻟﻤﻮﻗﻊ ) ‪( N‬ﻣﻊ اﻟﻌﻨﺼﺮ ﻓﻲ اﻟﻤﻮﻗﻊ )‬

‫‪( N-1‬وﻧﺒﺪل ﻣﻮﻗﻌﻬﻤﺎ ﺑﺤﻴﺚ ﻳﻜﻮن اﻻﺻﻐﺮ ﻗﺒﻞ اﻻﺧﺮ وﻧﺴﺘﻤﺮ ﻟﺤﻴﻦ‬

‫اﻟﻮﺻﻮل اﻟﻰ ﻣﻘﺎرﻧﺔ اﻟﻌﻨﺼﺮ ﻓﻲ اﻟﻤﻮﻗﻊ اﻟﺜﺎﻧﻲ ﻣﻊ اﻟﻌﻨﺼﺮ ﻓﻲ اﻟﻤﻮﻗﻊ اﻻول‬ ‫اﻟﻤﺮﺣﻠﺔ اﻟﺜﺎﻧﻴﺔ ‪-:‬‬ ‫ﻧﻘﺎرن ﺑﻨﻔﺲ اﻟﻄﺮﻳﻘﺔ اﻟﻌﻨﺼﺮ ﻓﻲ اﻟﻤﻮﻗﻊ ‪ N‬اﻟﻰ اﻟﻌﻨﺼﺮ ﻓﻲ اﻟﻤﻮﻗﻊ اﻟﺜﺎﻧﻲ‬ ‫اﻟﻤﺮﺣﻠﺔ اﻟﺜﺎﻟﺜﺔ ‪-:‬‬ ‫ﻧﻜﺮر اﻟﺨﻄﻮات اﻋﻼﻩ )‬

‫‪ ( N-1‬ﻣﻦ اﻟﺪورات‬

‫‪3‬‬

‫ﻣﻘﺎرﻧﺔ‬

‫ﻣﻘﺎرﻧﺔ‬

‫ﻣﻘﺎرﻧﺔ‬

‫ﻣﻘﺎرﻧﺔ‬

‫ﻣﻘﺎرﻧﺔ‬

‫ﻣﻘﺎرﻧﺔ‬

‫ﻣﻘﺎرﻧﺔ‬

‫اﻟﺪورة‬

‫اﻟﺪورة‬

‫اﻟﺪورة‬

‫اﻟﺪورة‬

‫اﻟﺪورة‬

‫اﻟﺪورة‬

‫اﻟﺪورة‬

‫اﻟﻘﺎﺋﻤﺔ‬

‫)‪(1‬‬

‫)‪(2‬‬

‫)‪(3‬‬

‫)‪(4‬‬

‫)‪(5‬‬

‫)‪(6‬‬

‫)‪(7‬‬

‫)‪(1‬‬

‫)‪(2‬‬

‫)‪(3‬‬

‫)‪(4‬‬

‫)‪(5‬‬

‫)‪(6‬‬

‫)‪(7‬‬

‫‪43‬‬

‫‪43‬‬

‫‪43‬‬

‫‪43‬‬

‫‪43‬‬

‫‪43‬‬

‫‪43‬‬

‫‪5‬‬

‫‪5‬‬

‫‪5‬‬

‫‪5‬‬

‫‪5‬‬

‫‪5‬‬

‫‪5‬‬

‫‪5‬‬

‫‪54‬‬

‫‪54‬‬

‫‪54‬‬

‫‪54‬‬

‫‪54‬‬

‫‪54‬‬

‫‪5‬‬

‫‪43‬‬

‫‪43‬‬

‫‪11‬‬

‫‪11‬‬

‫‪11‬‬

‫‪11‬‬

‫‪11‬‬

‫‪11‬‬

‫‪11‬‬

‫‪11‬‬

‫‪11‬‬

‫‪11‬‬

‫‪11‬‬

‫‪5‬‬

‫‪54‬‬

‫‪54‬‬

‫‪54‬‬

‫‪43‬‬

‫‪17‬‬

‫‪17‬‬

‫‪17‬‬

‫‪17‬‬

‫‪17‬‬

‫‪41‬‬

‫‪41‬‬

‫‪41‬‬

‫‪41‬‬

‫‪5‬‬

‫‪11‬‬

‫‪11‬‬

‫‪11‬‬

‫‪11‬‬

‫‪54‬‬

‫‪43‬‬

‫‪41‬‬

‫‪41‬‬

‫‪41‬‬

‫‪41‬‬

‫‪93‬‬

‫‪93‬‬

‫‪93‬‬

‫‪5‬‬

‫‪41‬‬

‫‪41‬‬

‫‪41‬‬

‫‪41‬‬

‫‪41‬‬

‫‪17‬‬

‫‪54‬‬

‫‪43‬‬

‫‪43‬‬

‫‪43‬‬

‫‪43‬‬

‫‪17‬‬

‫‪17‬‬

‫‪5‬‬

‫‪93‬‬

‫‪93‬‬

‫‪93‬‬

‫‪93‬‬

‫‪93‬‬

‫‪93‬‬

‫‪41‬‬

‫‪41‬‬

‫‪54‬‬

‫‪54‬‬

‫‪54‬‬

‫‪54‬‬

‫‪5‬‬

‫‪5‬‬

‫‪17‬‬

‫‪17‬‬

‫‪17‬‬

‫‪17‬‬

‫‪17‬‬

‫‪17‬‬

‫‪17‬‬

‫‪93‬‬

‫‪66‬‬

‫‪66‬‬

‫‪66‬‬

‫‪66‬‬

‫‪66‬‬

‫‪66‬‬

‫‪66‬‬

‫‪66‬‬

‫‪66‬‬

‫‪66‬‬

‫‪66‬‬

‫‪66‬‬

‫‪66‬‬

‫‪66‬‬

‫‪66‬‬

‫‪93‬‬

‫‪93‬‬

‫‪93‬‬

‫‪93‬‬

‫‪93‬‬

‫)(‪Public sub Bubble‬‬ ‫‪For i=1 to n-1‬‬ ‫‪For j= n to i+1‬‬ ‫)‪If a(j)
‫‪Shell Sort Algorithm‬‬ ‫ﺧﻮارزﻣﻴﺔ ﺗﺮﺗﻴﺐ ﺷﻴﻞ‬ ‫ﻓﻜﺮﺗﻬﺎ‪-:‬‬ ‫ﺗﻘﺴﻴﻢ اﻟﻘﺎﺋﻤﺔ اﻟﻰ ﻣﺴﺎﻓﺎت وهﻤﻴ ﺔ وﺗﺠ ﺮي ﻣﻘﺎرﻧ ﺔ ﻟﻜ ﻞ ﻋﻨ ﺼﺮﻳﻦ او اآﺜ ﺮ ﻟﻴ ﺴﺖ ﻣﺘﺠ ﺎورة واﻧﻤ ﺎ ﻣﺘﺒﺎﻋ ﺪة ﺑﻤ ﺴﺎﻓﺔ ﻣﺤ ﺪدة ﻣﻘ ﺪارهﺎ اﻟﻤ ﺴﺎﻓﺔ‬ ‫اﻟﻮهﻤﻴﺔ وهﻜﺬا ﻳﺘﻢ ﺗﻐﻴﺮ اﻟﻤﺴﺎﻓﺔ اﻟﻮهﻤﻴﺔ وﺗﺠﺮى ﻋﻤﻠﻴﺔ اﻟﻤﻘﺎرﻧﺔ واﻟﺘﻐﻴﺮ ﺣﺘﻰ ﺗﺘﺮﺗﺐ اﻟﻘﺎﺋﻤﺔ‬ ‫ﻣﺜﺎل ‪ -:‬رﺗﺐ ﻋﻨﺎﺻﺮ اﻟﻘﺎﺋﻤﺔ اﻟﺘﺎﻟﻴﺔ ﺑﺎﺳﺘﺨﺪام ﺗﺮﺗﻴﺐ ﺷﻴﻞ‬ ‫‪66‬‬

‫‪11‬‬

‫‪54‬‬

‫‪93‬‬

‫‪41‬‬

‫‪5‬‬

‫‪17‬‬

‫ﺧﻄﻮات اﻟﺤﻞ‪-:‬‬ ‫ﻻ‪ -:‬ﻧﺨﺘﺎر ﻣﺴﺎﻓﺔ وهﻤﻴﺔ ﻣﻘﺪرهﺎ‬ ‫او ً‬

‫‪Gap=4‬‬

‫وﻧﻀﻊ اﻟﻌﻨﺎﺻﺮ اﻻآﺒﺮ ﻋﻠﻰ ﺟﻬﺔ اﻟﻴﻤﻴﻦ واﻻﺻﻐﺮ ﻋﻠﻰ اﻟﻴﺴﺎر‬

‫‪4‬‬

‫‪43‬‬

‫ﺛﺎﻧﻴ ًﺎ ‪ -: -:‬ﻧﺨﺘﺎر ﻣﺴﺎﻓﺔ وهﻤﻴﺔ ﻣﻘﺪرهﺎ‬

‫‪66‬‬

‫‪11‬‬

‫‪54‬‬

‫‪93‬‬

‫‪41‬‬

‫‪5‬‬

‫‪17‬‬

‫‪43‬‬

‫‪66‬‬

‫‪11‬‬

‫‪54‬‬

‫‪93‬‬

‫‪41‬‬

‫‪5‬‬

‫‪17‬‬

‫‪43‬‬

‫‪66‬‬

‫‪11‬‬

‫‪54‬‬

‫‪93‬‬

‫‪41‬‬

‫‪5‬‬

‫‪17‬‬

‫‪43‬‬

‫‪66‬‬

‫‪11‬‬

‫‪54‬‬

‫‪93‬‬

‫‪41‬‬

‫‪5‬‬

‫‪17‬‬

‫‪43‬‬

‫‪Gap=2‬‬ ‫‪66‬‬

‫‪11‬‬

‫‪54‬‬

‫‪93‬‬

‫‪41‬‬

‫‪5‬‬

‫‪17‬‬

‫‪43‬‬

‫‪66‬‬

‫‪93‬‬

‫‪54‬‬

‫‪43‬‬

‫‪41‬‬

‫‪11‬‬

‫‪17‬‬

‫‪5‬‬

‫‪66‬‬

‫‪93‬‬

‫‪54‬‬

‫‪43‬‬

‫‪Result‬‬

‫ﺛﺎﻟﺜ ًﺎ ‪ -: -:‬ﻧﺨﺘﺎر ﻣﺴﺎﻓﺔ وهﻤﻴﺔ ﻣﻘﺪرهﺎ‬

‫‪41‬‬

‫‪11‬‬

‫‪17‬‬

‫‪5‬‬

‫‪Gap=1‬‬ ‫‪93‬‬

‫‪66‬‬

‫‪54‬‬

‫‪41‬‬

‫‪43‬‬

‫‪17‬‬

‫‪11‬‬

‫‪5‬‬

‫‪Quick Sort Algorithm‬‬ ‫ﺧﻮارزﻣﻴﺔ اﻟﺘﺮﺗﻴﺐ اﻟﺴﺮﻳﻊ‬ ‫ﻓﻜﺮﺗﻬﺎ‪-:‬‬ ‫اﺧﺘﻴﺎر ﻋﻨﺼﺮ ﻣﻦ ﻋﻨﺎﺻﺮ اﻟﻘﺎﺋﻤﺔ اﻟﻤﻌﻄﺎة ﻣﺜﻞ ‪) X‬اﻻﻓﻀﻞ ان ﻳﻜﻮن اﻟﻌﻨﺼﺮ اﻟﻮﺳﻄﻲ( ﺛﻢ ﻧﺠﻌﻞ اﻟﻌﻨﺎﺻﺮ اﻟﺘﻲ ﻗﻴﻤﺘﻬﺎ اﻗ ﻞ ﻣ ﻦ ‪ x‬ﻗﺒﻠ ﻪ ﻓ ﻲ‬ ‫اﻟﻘﺎﺋﻤﺔ اﻣﺎ اﻟﻌﻨﺎﺻﺮ اﻟﺘﻲ ﺗﻜﻮن ﻗﻴﻤﺘﻬﺎ اآﺒﺮ ﻣﻦ ‪ x‬ﺑﻌﺪﻩ ﺛﻢ ﺗﺘﻢ ﻋﻤﻠﻴﺔ اﻟﻤﺴﺢ اﻟﺘﺼﺎﻋﺪي ﻟﻌﻨﺎﺻﺮ اﻟﻘﺎﺋﻤﺔ اﻟﺘﻲ ﺗﻘ ﻊ ﻗﺒ ﻞ اﻟﻌﻨ ﺼﺮ ‪ x‬ﻓ ﺎذا وﺟ ﺪﻧﺎ‬ ‫ﻋﻨﺼﺮ اآﺒﺮ ﻣﻨﻪ ﻧﺤﺪدﻩ )أي ﻧﻀﻊ اﺷﺎرة( ﺛﻢ ﻧﻤﺴﺢ ﺗﻨﺎزﻟﻴًﺎ ﻋﻨﺎﺻﺮ اﻟﻘﺎﺋﻤﺔ اﻟﻮاﻗﻌﺔ ﺑﻌﺪ ‪ x‬ﻓﺎن وﺟﺪﻧﺎ ﻋﻨﺼﺮ اﻗﻞ ﻣﻨﻪ ﻧﺤ ﺪدﻩ اﻳ ﻀًﺎ ﺛ ﻢ ﻧﺠ ﺮي‬ ‫ﻋﻤﻠﻴﺔ اﻟﺘﺒﺪﻳﻞ ﺑﻴﻨﻬﻤﺎ وﻧﺴﺘﻤﺮ ﺣﺘﻰ ﺗﺘﺮﺗﺐ اﻟﻘﺎﺋﻤﺔ‪.‬‬ ‫ﻣﺜﺎل ‪ -:‬رﺗﺐ ﻋﻨﺎﺻﺮ اﻟﻘﺎﺋﻤﺔ اﻟﺘﺎﻟﻴﺔ ﺑﺎﺳﺘﺨﺪام اﻟﺘﺮﺗﻴﺐ اﻟﺴﺮﻳﻊ‬ ‫‪66‬‬

‫‪5‬‬

‫‪17‬‬

‫اﻻآﺒ ﺮ‬

‫‪41‬‬

‫‪93‬‬ ‫‪X‬‬

‫‪5‬‬

‫‪11‬‬

‫‪54‬‬ ‫اﻻﺻ ﻐﺮ‬

‫‪43‬‬

‫ﺧﻄﻮات اﻟﺤﻞ‪-:‬‬ ‫اﻟﻤﺮﺣﻠﺔ اﻻوﻟﻰ‪:‬‬ ‫‪X= 41‬‬

‫ﻻ‪ -:‬ﻧﺨﺘﺎر‬ ‫او ً‬ ‫‪41<66‬‬

‫اذن ﺗﺒﻘﻰ ﻣﻜﺎﻧﻬﺎ‬

‫‪41 rel="nofollow">5‬‬

‫اذن ﻧﻀﻊ اﺷﺎرة ﻋﻠﻰ اﻟـ ‪5‬‬

‫‪ 41<43‬اذن ﻧﻀﻊ اﺷﺎرة ﻋﻠﻰ اﻟـ ‪43‬‬ ‫ﻧﺠﺮي ﻋﻤﻠﻴﺔ اﻟﺘﺒﺪﻳﻞ ﻓﺘﺼﺒﺢ اﻟﻘﺎﺋﻤﺔ هﻲ‪-:‬‬ ‫‪66‬‬ ‫‪41>17‬‬

‫اذن ﻧﻀﻊ اﺷﺎرة ﻋﻠﻰ اﻟـ ‪17‬‬

‫‪41<54‬‬

‫اذن ﻧﻀﻊ اﺷﺎرة ﻋﻠﻰ اﻟـ ‪54‬‬

‫‪43‬‬

‫‪17‬‬

‫‪93‬‬

‫‪11‬‬

‫‪41‬‬

‫‪5‬‬

‫‪54‬‬

‫ﻧﺠﺮي ﻋﻤﻠﻴﺔ اﻟﺘﺒﺪﻳﻞ ﻓﺘﺼﺒﺢ اﻟﻘﺎﺋﻤﺔ هﻲ‪-:‬‬ ‫‪66‬‬ ‫‪41<93‬‬

‫اذن ﺗﺒﻘﻰ ﻣﻜﺎﻧﻬﺎ‬

‫‪41>11‬‬

‫اذن ﺗﺒﻘﻰ ﻣﻜﺎﻧﻬﺎ‬

‫‪43‬‬

‫‪54‬‬

‫‪93‬‬

‫‪11‬‬

‫‪41‬‬

‫‪5‬‬

‫‪17‬‬

‫اﻟﻤﺮﺣﻠﺔ اﻟﺜﺎﻧﻴﺔ‪:‬‬ ‫ﻻ‪ -:‬ﻧﺄﺧﺬ ﻗﺎﺋﻤﺔ اﻻﻋﺪاد اﻟﺘﻲ هﻲ اآﺒﺮ ﻣﻦ‬ ‫او ً‬

‫‪X‬‬

‫‪43‬‬

‫‪66‬‬

‫‪93‬‬

‫‪54‬‬

‫ا‪ .‬ﻧﺨﺘﺎر ﻟﻬﺎ ‪X=54‬‬

‫‪54<66‬‬

‫اذن ﺗﺒﻘﻰ ﻣﻜﺎﻧﻬﺎ‬

‫‪54>43‬‬

‫اذن ﻧﻀﻊ اﺷﺎرة ﻋﻠﻰ اﻟـ ‪43‬‬

‫‪54<93‬‬

‫اذن ﻧﻀﻊ اﺷﺎرة ﻋﻠﻰ اﻟـ ‪93‬‬ ‫‪66‬‬

‫ﻧﺠﺮي ﻋﻤﻠﻴﺔ اﻟﺘﺒﺪﻳﻞ ﻓﺘﺼﺒﺢ اﻟﻘﺎﺋﻤﺔ هﻲ‪-:‬‬ ‫ب‪ .‬ﻧﺨﺘﺎر‬

‫‪X=93‬‬

‫‪93‬‬

‫‪43‬‬

‫‪54‬‬

‫ﺑﺎﻟﻨﺴﺒﺔ اﻟﻰ اﻟﻘﺎﺋﻤﺔ‬ ‫‪93‬‬

‫‪66‬‬ ‫ﻓﺘﺼﺒﺢ اﻟﻘﻠﺌﻤﺔ ‪-:‬‬ ‫‪93‬‬

‫‪66‬‬

‫‪43‬‬

‫‪54‬‬

‫ﻧﻄﺒﻖ ﻧﻔﺲ اﻟﺨﻄﻮات ﺑﺎﻟﻨﺴﺒﺔ اﻟﻰ اﻟﻘﺎﺋﻤﺔ‬ ‫‪11‬‬

‫‪6‬‬

‫‪17‬‬

‫‪5‬‬

-:‫ﻣﻼﺣﻈﺎت ﻋﺎﻣﺔ‬ ‫ ﻋﻨﺪﻣﺎ ﻧﻀﻊ اﺷﺎرة ﻋﻠﻰ ﻋﻨﺼﺮ ﻓﺎﻧﻪ ﻳﻨﺘﻘﻞ اﻟﻰ اﻟﺠﻬﺔ اﻻﺧﺮى ﻣﻦ اﻟﻘﺎﺋﻤﺔ‬.1 ( 2N*Ln N

) ‫ ﻳﻜﻮن اآﺒﺮ ﻋﺪد ﻣﻦ اﻟﻤﻘﺎرﻧﺎت اﻟﻰ هﺬﻩ اﻟﻄﺮﻳﻘﺔ هﻮ‬.2

‫ هﺬﻩ اﻟﻄﺮﻳﻘﺔ ﺗﻜﻮن ﻣﻔﻴﺪة اﻟﻰ ﻟﻠﻘﻮاﺋﻢ اﻟﺘﻲ ﺗﻜﻮن ﻏﻴﺮ ﻣﺮﺗﺒﺔ اﻟﻰ ﺣﺪ آﺒﻴﺮ وﻏﻴﺮ ﻋﻤﻠﻴﺔ ﻟﻠﻘﻮاﺋﻢ ﺷﺒﻪ اﻟﻤﺮﺗﺒﺔ‬.3 Public sub Quicksort(byref list as string, byref F as integer, byref L as integer ) Dim I as integer Dim j as integer Dim x as integer{or any other type} I=f J=L X=list((i+j) / 2) Repeat While list(i)<x do i=i+1 While x< list(j) do j=j+1 If i<=j then Swap(list(i),list(j)) I=i+1 J=j+1 End if Loop untie i<j If f<j then Quicksort(list,f,j) End if If i
7

Related Documents

Lecture 18
November 2019 17
Lecture 18
May 2020 18
Lecture 18
April 2020 15
Lecture 18
October 2019 19
Lecture 18
October 2019 8
Lecture 18
May 2020 4